Saturday, 6 August 2016

jquery - JavaScript is it possible to use var in your .html




Is it possible to use var codes in your HTML file but in a .js file you can have the rest of the script?




for example my code is










this



img




but is it possible to do this, if so how do I tell JavaScript to search for the id's between the Body Tags because the script is in the Head Tag and it only searches in the head tag but I don't wont the script in the body tag because it makes it easier to access in the head tag if this is not possible then is it possible to include the var in the html file and the script below the vars in a separate .js file? My example below should explain better.



html file










this



img





js file



// First Image
image.src = "your image link for first image here";
link.title = "Movie title ex Batman (2008)";
tooltipname.href = "link to site when movie is clicked";
namebelowimg.innerHTML = "movie name here";
// End First Image



If you need a better example go here


Answer



You getElementById won't work because scripts are executed before body is rendered. Simplest way to make it work is to move script tag after the body.



Other way might be to wait for body to load





No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...