Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

executing javascript function from HTML without event

I wish to call a javascript function from an HTML page and I do not want it dependent on any event. The function is in a separate .js file since I wish to use it from many web pages. I am also passing variables to it. I've tried this:

HTML:

<script type="text/javascript" src="fp_footer2.js">
footerFunction(1_basic_web_page_with_links, 1bwpwl.html);
</script>    

The function in fp_footer2.js:

function footerFunction(path, file) {

document.write("<a href=" + path + "/" + file + " target='_blank'>Link to the original web page for this assignment.</a>");

return;
}

(I have also tried putting the fp_footer2.js file reference in the header, to no avail. I'm not sure if I can put it 'inline' like I did in this example. If not, please let me know.

PS: I know I can do this with a simple 'a href=""' in the HTML itself. I wanted to see if this could work, for my own curiosity.

like image 431
user1800823 Avatar asked Oct 25 '25 14:10

user1800823


1 Answers

If a <script> has a src, then the external script replaces the inline script.

You need to use two script elements.

The strings you pass to the function also need to be actual strings and not undefined variables (or properties of undefined variables). String literals must be quoted.

<script src="fp_footer2.js"></script>
<script>
    footerFunction("1_basic_web_page_with_links", "1bwpwl.html");
</script>    
like image 84
Quentin Avatar answered Oct 28 '25 05:10

Quentin