0

I have some js code that has been imported from another file which has a variable in it I want to update in my HTML file. How can I update the variable without pasting the js code into my HTML file?

JS (script.js):

var link = "https://example.com"
var codeBlock = `...some html code... ${link} ...some more html code`;

document.getElementById("div").innerHTML = codeBlock

HTML:

<div id="div"></div>
<script src= "script.js"></script>
<script>
    //I want to change this variable
    var link = "https://anotherexample.com"
</script>
6
  • 1
    Unable to get what your question is. Commented Apr 1, 2019 at 12:59
  • try using <script src="name of javascript.js"></script> Commented Apr 1, 2019 at 12:59
  • 1
    Please elaborate the code in more detail. Its hard to understand exact meaning Commented Apr 1, 2019 at 13:00
  • 1
    You should include the javascript file. You can create a new javascript file, then include it as a script either in the head or the body Commented Apr 1, 2019 at 13:01
  • Does this mean like, how can i change my HTML link variable value runtime based on external file variable ? Commented Apr 1, 2019 at 13:07

3 Answers 3

1

On line one of your code, you declare link and give it a value.

On line two, you use that value.

You can't change link later on and affect what happened when the value was read and used in the past.

If you want to change the final result, then it is the final result you have to change (i.e. you need to assign a new value to document.getElementById("div").innerHTML).

Sign up to request clarification or add additional context in comments.

4 Comments

could you please explain further on this?
@DuncanLeung — What else is there to explain? It's just linear time. One thing happens after another. You can't time travel to change something before it affected something else.
no, like the part about assigning a new value to document.getElementById("div").innerHTML
document.getElementById("div").innerHTML = somethingNew
1

You are getting the value but not setting it to the variable that the HTML is showing.Is this you are looking for :

var updatedLink = document.getElementById("anotherlink").innerHTML;
var codeBlock = `...some html code... ${updatedLink} ...some more html code`;

document.getElementById("div").innerHTML = codeBlock
<div id="div"></div>
<div id="anotherlink" style="display:none"></div>
<script src= "script.js"></script>
<script>
    var link = "https://anotherexample.com";
    document.getElementById("anotherlink").innerHTML = link
</script>

6 Comments

yes, but chrome shows this error: ``` bottombar.js:1 Uncaught TypeError: Cannot read property 'innerHTML' of null at bottombar.js:1 ```
Yes, but chrome gives me these errors: script.js:1 Uncaught TypeError: Cannot read property 'innerHTML' of null at script.js:1 and Uncaught TypeError: Cannot set property 'innerHTML' of null at ______:115
@DuncanLeung yes, you will get this error unless you include required scripts either in the head or in body.
so the javascript in script.js has to be in the html page, right?
Yes you have to include the js file in your page.
|
0

Your script is working and the variable is being changed. If you console.log(link) after declaring it again, you'll see the variable has changed. If you're expecting the script to execute again, that won't happen without calling it again.

What you may want to try is iterating through the document looking for something to trigger that statement, for example, links in the document.

const links = document.querySelectorAll('a');  
var href = [];
links.forEach(link => href.push(link.href));
var codeBlock = '';

href.forEach(link => {
  if (link == "https://stacksnippets.net/anotherexample.com") {
    codeBlock = `...some html code... ${link} ...some more html code`;
    document.getElementById('div').innerHTML = codeBlock;
  }  
})
<div id="div"><a href="anotherexample.com">link</a></div>

Or without checking for a specific URL:

const links = document.querySelectorAll('a');  
var href = [];
links.forEach(link => href.push(link.href));
var codeBlock = '';

href.forEach(link => {
  codeBlock = `...some html code... ${link} ...some more html code`;
  document.getElementById('div').innerHTML = codeBlock;     
})
<div id="div"><a href="anotherexample.com">link</a></div>

2 Comments

if I have multiple pages that need this code, do I need to make multiple if statements for each of them? is there and easier way?
You don't actually need the if statement at all. That's just an example to target a specific url

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.