1

Is there any possibility to run javascript only when the button has been clicked. I don't care if the load might take a bit, it's just that I dont need it to load with the page.

So for example we have a button:

<div id="proceed"></div>

Then when that button has been clicked we have a popup window with 2 more buttons.

So one of the buttons from popup will be like confirm run it and second one will simply close popup.

And what I want is to run following script src:

src="http://fileice.net/gateway/mygate.php?id=4465766c4278366467523838"

When the "proceed" has been clicked. So script will have time to load up till user read what popup message says.

So... any chances to run script src on button click?

EDIT:

The gateway conflicts with some other scripts and thats the main reason why to run it on click only.

dvlden
  • 2,402
  • 8
  • 38
  • 61

5 Answers5

3

Without JQuery:

<input type="button" onclick="loadScript('http://fileice.net/gateway/mygate.php?id=4465766c4278366467523838');" value="Proceed" />

In your Javascript:

function loadScript(script_url)
{
    var head= document.getElementsByTagName('head')[0];
    var script= document.createElement('script');
    script.type= 'text/javascript';
    script.src= script_url;
    head.appendChild(script);
}
N. Taylor Mullen
  • 18,061
  • 6
  • 49
  • 72
2

Use jQuery:

function doAction() {
    $.getScript("http://fileice.net/gateway/mygate.php?id=4465766c4278366467523838");
    return false;
}

HTML:

<div id="proceed" onClick="doAction()"></div>
Edward Ruchevits
  • 6,411
  • 12
  • 51
  • 86
2

In jQuery:

$('#proceed').click(function(){
    $.getScript('http://fileice.net/gateway/mygate.php?id=4465766c4278366467523838');
});
j08691
  • 204,283
  • 31
  • 260
  • 272
  • Quite interesting and simple, but conflicts with jQuery and I can't run it. – dvlden Aug 09 '12 at 19:21
  • 1
    How does it conflict with jQuery? It is jQuery. – j08691 Aug 09 '12 at 19:25
  • My mistake. I left jQuery and removed an youtube script instead. It was conflict with another jquery script. (Gateway includes jQuery inside thats why I thought jQuery conflicts with older version of jQuery - 2x jQuery included) – dvlden Aug 09 '12 at 20:04
1

maybe you can get your answer here .. How to dynamically insert a <script> tag via jQuery after page load?.. what they are doing on load you can do it on button click

Community
  • 1
  • 1
codisfy
  • 2,155
  • 2
  • 22
  • 32
1

I think it's worth noting that the script tag also has a defer attribute which

Delays the execution of a script

until after the page is loaded. More

It might be a viable option if you don't want to have to manually be involved to load the script and you can load it after the rest of the page is ready.

Best of luck!

ChrisHarris2012
  • 404
  • 2
  • 6
  • Defer simply tries to delay execution of a script until the page has loaded. The OP wants to run the script on the click of a div. – j08691 Aug 09 '12 at 19:18
  • The OP said '...it's just that I dont need it to load with the page.' Defer, according to [this question](http://stackoverflow.com/questions/5250412/how-exactly-does-script-defer-defer-work) is usually occurs when the document is fully loaded. I was simply offering an alternative since his request for a 'clickable load' functionality seemed like a work around for the problem – ChrisHarris2012 Aug 09 '12 at 19:26