0

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!

2
  • So I need to take out this js code but I do not know how to do that. - edit the file with your choice of editor ... remove the unwanted code ... profit Commented Feb 1, 2017 at 23:16
  • :) think I did not try? I tried ... and I prayed .... Still no answer :D Commented Feb 1, 2017 at 23:21

2 Answers 2

4

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>
Sign up to request clarification or add additional context in comments.

4 Comments

Exactly this is my idea, but it will not work if it the code is the same
I think the problem you're having is that you're executing elm before the other script has loaded, you need a method to delay the execution of your call until the other file is loaded.
Yes! Exactly! So I decided to put all my inline js in a separate file which is loaded after the scripts I need, but then I need to modify the initial inline javascript which is not my filed and I really need help
The reason why the other scripts are executed not on time is that my SEO insists on making them load asynchronously. So this is a problem
-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

4 Comments

If I understand you right, this is how it is done at the moment. But I need to rewrite this so it works from another file like @databyss suggested
If you want to access from another file. 1- Then create a new js file. 2- Write your function in the new file. 3- Attach that js file in the file you are working. 4- Now call the function declared in the new js file in the file you are working. Hope it make sense.
Do you think if I write this code the same as here it will execute? Because this is the problem - it does not and this is understandable.
If I could do all of that I would not write the question for sure ;)

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.