Jump to content

Calling Javascript function from html head


phpgoal

Recommended Posts

Hello,

 

I am calling JS funciton from HTML head. I want to see 'Click' button only if the date is equal to 'June 2013'. If the date is not equal to 'June 2013', I dont want to see 'Click' button.

 

Not working. Please help!

 
<html>
<head>
    <script type="text/javascript">
 
var datestr =.............
 
if (datestr == 'June 2013'){<button 
onclick="myFunction1('MS','QA')">Click</button>
    </script>
 
</head>
 
<body>
 
<SCRIPT LANGUAGE="JavaScript">function 
myFunction1(name,job){alert("Welcome " + name + ", the " + 
job);}
 

</script>
 
 
</body>
</html>

 

<html>
    <head>
        <title>Xaotique - JS Page</title>
        
        <script type="text/javascript">
            // Wait for the page to load before executing JS.
            window.addEventListener("load", function()
            {
                // Set the datestring for comparison.
                var datestr = window.document.querySelector("#datestr").innerHTML;
                
                // Compare it ..
                if (datestr == "June 2013")
                {
                    // Create the button and set attributes.
                    var myButton = window.document.createElement("button");
                        myButton.innerHTML = "Click Me!";
                        myButton.id = "myButton";
                    
                    // Append the button to the body.
                    window.document.body.appendChild(myButton);
                    
                    // Wait for the button to be clicked.
                    myButton.addEventListener("click", function()
                    {
                        var name = "MS", job = "QA";
                        
                        alert("Welcome " + name + ", the " + job + "!");
                    });
                }
            }, false);
        </script>
    </head>
    
    <body>
        <p id="datestr">June 2013</p>
    </body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.