Javascript

Dynamically Changing src Attribute of script

I had a situation where I wanted to change the src of a script dynamically before the script runs.
Given the following code, we needed to change src before the script gets executed:

<html>
    <body>
        <script language="JavaScript" type="text/javascript" src="..."></script>
    </body>
</html>

So simply (This simply took a couple of hours!) I did this:

<script language="JavaScript" type="text/javascript">
    url = ....
    function staticLoadScript() {
            document.write('<script language="JavaScript" type="text/javascript" src="', url, '"><\/script>');
           }
        staticLoadScript();
</script>

setInterval

setTimeout() triggers expression only once but setInterval() keeps triggering expression again and again (unless you tell it to stop).

window.setInterval("function()",300);
window.setInterval(function showAnn(){/*do something*/ },3000);

Stop submit with js

function test(){

    if(document.getElementById('postalRate').value== '') {
        alert("calculate rate") ;
    }
    else window.location = "purchase.htm?method=showPurchaseSummary";
}
</script>

<input type="button" onclick="test();" value="Buy It Now"/>

Submit a form using js

document.myform.submit(); where myform is the form name.

Confirm

function confirmDel(){
    var answer = confirm("delete?")
    if (answer){
        window.location = "..."
    }else{

    }
}

Submit different actions in one form

<script>
function submitThis(target){
    if(target == 'free')     document.myform.action="newAd.htm?method=saveFreeAd";
    if(target == 'pay')     document.myform.action="newAd.htm?method=payNow";
    if(target == 'save')     document.myform.action="newAd.htm?method=saveAndPayLater";
    if(target == 'preview') document.myform.action="newAd.htm?method=previewAd";
    document.myform.submit();
}
</script>

<form:form method="post" name="myform">
    <input type="button" value="Save Free Ad"         onclick="javascript:submitThis('free')"/>
    <input type="button" value="Pay Now"             onclick="javascript:submitThis('pay')"/>
    <input type="button" value="Save and Pay Later" onclick="javascript:submitThis('save')"/>
    <input type="button" value="Preview"             onclick="javascript:submitThis('preview')"/>
</form:form>
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License