简体   繁体   中英

How to reload javascript without refreshing the page?

I have a webpage that links some javascript via tags. The script is amazon-localiser.js which will change an amazon link to one appropriate for the visitor. eg an Amazon.com link will swap to amazon.co.uk for a UK visitor or Amazon.de for a german visitor.It also appends to the link the relevant amazon affiliate link.

When the user lands on the page they click through some options (javascript) however by the time you reach an amazon link the page must be refreshed for the amazon-localiser.js script to work. I have tried using a page refresh in HTML but this sends me back to the very beginning of the questions. How do I reload the javascript without affecting the users location on the site?

The site is www.wfbsir.com, if you select "Scifi" then "Maybe" you will get to an amazon.com link, if you hover over it you will see it links to amazon.com if you refresh the page it will show you the link to your local store with an affiliate link appended.

html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>What book should I read?</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="manifest" href="/manifest.json">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="msapplication-TileImage" content="/ms-icon-144x144.png">
    <meta name="theme-color" content="#ffffff">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    <link rel="stylesheet" href="app.css" />

  </head>
  <body>
<div class="wrapper">
  <div class="container">
    <div class="row">
      <div class="col-xs-12 text-right">
        <button class="btn btn-default btn-corner" type="submit" data-bind="click: startOver, visible: queryData().id > 0">Start over</button>
      </div>
    </div>
  </div>

  <div class="container main">
    <div class="row">
      <div class="c12 text-center">
        <h1 data-bind="text: queryData().text"></h1>
        <h3 data-bind="text: queryData().subhead"></h3>
        <h3><a data-bind="text: queryData().link, attr: {href: url}"></a></h3>
        <div class="option-group" data-bind="foreach: queryData().answers">
          <button class="btn btn-default btn-lg" type="submit" data-bind="click: $parent.goToTarget, text: text"></button>
        </div>
        <button class="btn btn-default" type="submit" data-bind="click: stepBack, visible: navHistory().length > 1">Previous Step</button>
                            <button class="btn btn-default" type="submit" data-bind="click: buyBook, visible: navHistory().length > 1">Buy the book</button> 
             </div>
    </div>
  </div>
  <div class="push"></div>
</div>


<script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-3.3.0.js"></script>
<script src="app.js?v=0.4.0"></script>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="amazon-localiser.js"></script>
<script>

</script>

I have tried using jQuery getScript and also window.location.reload(); but neither reload just the javascript, the only thing that I can find to work is F5/Refresh.

I noticed that the amazon-localiser.js invokes the function findLocation onload of the page, as you can see below.

if (window.addEventListener) {
    window.addEventListener("load", findLocation, false)
} else {
    window.attachEvent("onload", findLocation)
}

So, a possible solution to your problem, could be to invoke this function again when you need to update your amazon link. I tried invoking it from the console and it works, so try to invoke findLocation() manually when needed and see if it serves your scope.

Simone

You can add dynamically script on the page with some condition, for example:

var script = document.createElement('script');
var src;

if (true) {
  src = 'amazon.co.uk';
} else {
  src = 'amazon.com';
}
script.src = src;
document.head.appendChild(script);

As gnllucena told, you can view the question or there is the solution.

  1. Build the loader function:

Put the following code in the document

<script type="text/javascript">
function LoadMyJs(scriptName) {
var docHeadObj = document.getElementsByTagName("head")[0];
var newScript= document.createElement("script");
newScript.type = "text/javascript";
newScript.src = scriptName;
docHeadObj.appendChild(newScript);
}
</script>


// place a button for reloading script.

<input type="button" name="reloadNewJs" value="Reload JavaScript" onClick="LoadMyJs('needed_script.js')">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM