Dimentica
Dimentica

Reputation: 805

Execute inline javascript in another file

In my site I have inline javascript.

I have this in my header:

<script type="text/javascript">
        function eml(person, domain) {
            document.write('<'+'a h'+'ref="ma'+'ilto'+':'+person+'@'+domain+'">'+person+'@'+domain+'</a>');
</script>

And this in the body:

<script type="text/javascript">
    eml('info','site.com');</script>
<noscript><span class="eml">deni.elpar@com</span></noscript>

So I need to make this javascript not inline and execute it from a js file. However, this function, as it is at the moment, does not work if executed from a different file, so I need suggestions on how to rewrite it. Thanks!

Upvotes: 0

Views: 1266

Answers (2)

databyss
databyss

Reputation: 6488

You would copy just the js to another file, let's say newfile.js, for example.

Then in your html page add the tag:

<script src="newfile.js"></script>
<script type="text/javascript">
    // execute function after resources are loaded
    window.onload = function() {
        eml('info','site.com'); 
    }
</script>

Upvotes: 4

Rohit Agarwal
Rohit Agarwal

Reputation: 1

I will suggest you take that header part and write all code in the same script tag in a body section.

function eml(person, domain) { document.write(''+person+'@'+domain+''); eml('info','site.com'); deni.elpar@com

Upvotes: -1

Related Questions