0

I have a script that calls a JS file to display some contents and it works only when the page is reloaded, I mean, when I switch between the different pages that I have in my app, I can't see the contents only unless I reload the whole page - that is when it appears again.

I tried to call the script whenever the user switches between the pages but it didn't work.

The easiest way is to reload the page in background.

This is the function that I want to be called

loadMyScript(event) {
  this.loadJsFile('myScript.js');
}

How can I execute the JS or load the page in background with Angular 5?

2
  • 1
    "I have a script that calls a JS file to display some contents and it works only when the page is reloaded" - rewrite it so it works sensibly with your SPA. Commented Jul 2, 2019 at 11:07
  • Can you provide a code sample on how you 'tried to call the script'? Commented Jul 2, 2019 at 11:15

2 Answers 2

0

You can add script tag with src="..." on your page body with .insertAdjancentHTML for example. Or you can use AMD.

Also you can try eval but it is BAD PRACTICE

Sign up to request clarification or add additional context in comments.

2 Comments

Please add minimal code examples to make the answer easier to understand.
is it possible to reload the page on background ?
0

i am working laravel with vue js. and i need this load js after page load or in background.

so i find this solution hope this will helpful for you.

<script>
window.onload=function () {
    setTimeout(() => {
       loadGoogleCaptcha();
    }, 2000); 

}
function loadGoogleCaptcha() {
 var script = document.createElement("script");
 script.src = "http://www.google.com/recaptcha/api.js";
 script.type = "text/javascript";
 document.getElementsByTagName("head")[0].appendChild(script);
}
</script>

4 Comments

the problem is that the REFRESH of the whole page is required to make the script shows the data; otherwise, it just calls the script and shows a 200 OK as a response, but what i wanted to do is to reload the page in background, as if we press F5 ^^
You can do make your request running in background using the webworkers.
any examples of how to use it ?
process search on google about web-workers and you get knowledge.

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.