0





17

Advertisement

I have 2 pages. one.html and two.html

The first html page contains a script tag with a java script function modify(d).In two.html i want to call this same function instead of retyping it again.How can i do this? Do i need to create a link between the pages? if yes, How?

I have tried using in the second file. But i keep getting the error: "modify(d) not defined" in the console.

Both of the files are placed next to each other in my folder structure so the path should be correct.

Thank you in advance.

Question author Sarah333 | Source

Advertisement


0


If you drop inline scripts (and it goes for inline styles too) and do like this, where you store functions (and rules) in a file of its own, you will be able to reuse it (them) like shown in below sample

File: one.html

<!DOCTYPE html>
<html>
    <head>
        <title>Page One</title>
        <meta http-equiv='content-type' content='text/html; charset=UTF-8' />
        <link rel='stylesheet' type='text/css' href='css/style.css' />
        <script src="js/script.js"></script>
    </head>
<body>
    Page one content  
</body>
</html>

File: two.html

<!DOCTYPE html>
<html>
    <head>
        <title>Page two</title>
        <meta http-equiv='content-type' content='text/html; charset=UTF-8' />
        <link rel='stylesheet' type='text/css' href='css/style.css' />
        <script src="js/script.js"></script>
    </head>
<body>
    Page two content  
</body>
</html>

File: style.css

body {
  background: url(../images/bkg.png);
}

File: script.js

function modify(d) {
  ....
}

And have your files located like this in your folder

/www/one.html
/www/two.html
/www/js/script.js
/www/css/style.css
/www/images/bkg.png
Answer author Lgson