0

I was trying to make a button that will run a JavaScript when it was clicked.First, my script was like this

<script>
    function myFunction()
    {
        alert('Invalid username or password. Please try again.')
    }
</script>

<input onclick="myFunction()" type="submit" value="Generate" href="submit.php" >

It works, and a popup appear.But when I try to call JavaScript from another site, nothing happens.

<script>
    function myFunction()
    {            
        document.body.appendChild(document.createElement('script')).src='https://raw2.github.com/BlackEagleBCC/Script/master/myscript.js';
    }
</script>

<input onclick="myFunction()" type="submit" value="Generate" href="submit.php" >
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Code Newbie
  • 124
  • 1
  • 2
  • 10

2 Answers2

0

You need to include the js file once event occurs.

Please refer to this link, it demonstrates dynamic including js and css.

Add html code

<ul>
 <li><a href="javascript:loadjscssfile('myscript.js','js')">Load "myscript.js"</a></li>
 <li><a href="javascript:loadjscssfile('mystyle.css','css')">Load "mystyle.css"</a></li>
</ul>

Then js code

function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}

Code for myscript.js

var petname="Spotty"
alert("Pet Name: " + petname)

Code for mystyle.css

#ul li{
background-color: lightyellow;
}

Similar to this :

You can call loadjscssfile('myscript.js','js') inside your button onclick() event handler.

Mazzu
  • 2,799
  • 20
  • 30
0

Update: Try this out:

<a id='myScript'  type="submit"  href="submit.php" > Generate </a>

Script:

<script type="text/javascript">
    var myScript = document.getElementById('myScript');

    myScript.onclick = function(){

        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "https://raw2.github.com/BlackEagleBCC/Script/master/myscript.js"; 
        document.getElementsByTagName("head")[0].appendChild(script);
        return false;
    }
</script>

EDITED: The below solution might not be what you are looking for now.

I have been using this,a solution which I found from stackoverflow itself.

function loadScripts(array,callback){
    var loader = function(src,handler){
        var script = document.createElement("script");
        script.src = src;
        script.onload = script.onreadystatechange = function(){
        script.onreadystatechange = script.onload = null;
            handler();
        }
        var head = document.getElementsByTagName("head")[0];
        (head || document.body).appendChild( script );
    };
    (function(){
        if(array.length!=0){
            loader(array.shift(),arguments.callee);
        }else{
            callback && callback();
        }
    })();
}

This will help you create script tags.

loadScripts([
   "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js",
   "https://raw2.github.com/BlackEagleBCC/Script/master/myscript.js"
],function(){
    alert('Scripts have been loaded successfully');
});

Edited: I have used this method when I needed to build script tags and get a callback after everything loads fine. (These things come handy :) ) https://stackoverflow.com/a/1867135/3222041

Community
  • 1
  • 1
newTag
  • 2,149
  • 1
  • 22
  • 31
  • If you copy paste a code block from an other thread, even a long time ago, can you edit your post and indicating thread where you found it please ? – franchez Feb 12 '14 at 07:19
  • 1
    @franchez Sorry.. a little late.. Yes of course..I have added it along with the answer. Thanks :) – newTag Feb 12 '14 at 07:35
  • @ScriptKiddies I have added the link I referred..Hope it helps you. – newTag Feb 12 '14 at 07:38
  • @newTag thanks a lot. but i still can't figure out how to use it. – Code Newbie Feb 12 '14 at 07:45
  • @ScriptKiddies I have provided a simple solution than the previous one.(previous solution which I think will not be needing for you now unless you have multiple scripts to load). – newTag Feb 12 '14 at 08:34