Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to utilize javascript in the anchor href and not navigate away from the page
    primarykey
    data
    text
    <p>I need to load some dependency java-scripts onto a page if they don't already exist, through java-script. This is done by checking to see if variables are defined, if they are not then generate a script element and add it to the head of the page. </p> <p>It works the way I'd expect in Chrome. It seems to open a new window when I call the code directly in the href in IE/Firefox. Oddly it works the way I'd expect when I call it indirectly in the href. Here's a trimmed example showing the behaviors (with loading jquery)</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script&gt; function test(){ if (typeof jQuery == 'undefined') { var js = document.createElement('script'); js.setAttribute('type','text/javascript'); js.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'); var head= document.getElementsByTagName('head')[0]; head.appendChild(js); } } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;a href="javascript: if (typeof jQuery == 'undefined') {var js = document.createElement('script');js.setAttribute('type','text/javascript');js.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');var head= document.getElementsByTagName('head')[0];head.appendChild(js);}"&gt; navigates to new page &lt;/a&gt; | &lt;a href="javascript: test();"&gt;executes on page&lt;/a&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>If you view this page you'll notice the second link actually loads jquery and puts it on the dom. It also works in chrome. On IE/FF you'll navigate to a new page with <code>&lt;body&gt;[object HTMLScriptElement]&lt;/body&gt;</code> as the body contents. </p> <p>I am perplexed as to why it behaves differently for the second link, and am looking for a solution.</p> <p>FYI: The reason this needs to be executed in a url is because I'm actually calling this within an AS2 SWF using getURL(), and I cannot alter the pages html to include the java-scripts necessary.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload