Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
this is my first time to write anything in jscript file, so please i need help

this is the function i wrote in jscript

C#
function SelectPage(DocURL) {
    var ID = document.URL;

    switch (DocURL) {
        case "Who We Are.aspx":
            document.getElementById("Td1").className = "itemHovered";
            break;
        case "Services.aspx":
            document.write("Super Saturday");
            break;
        case "Products.aspx":
            document.write("Sleepy Sunday");
            break;
        case "Clients.aspx":
            document.write("Sleepy Sunday");
            break;
        case "ContactUs.aspx":
            document.write("Sleepy Sunday");
            break;
        default:
            document.write("I'm looking forward to this weekend!");
    }

i want to call this fun on pageload
how can i do that
thankx in advance
Posted

You'll have to use 'Pagemethods' if you wanna call JS from CodeBehind in an aspx.. more info here:

http://www.google.com/search?gcx=w&sourceid=chrome&ie=UTF-8&q=pagemethods[^]


if you just want this code to run after page is ready, use Jquery and type

$(document).ready(function() {

//your code here


});
 
Share this answer
 
Comments
eman88 2-Mar-12 12:10pm    
thank u for ur answer :)
[no name] 2-Mar-12 12:18pm    
My pleasure
There are several ways. You can use
C#
Page.ClientScript.RegisterStartupScript(this.GetType(), "AnyThing", "SelectPage();", true);

or in your aspx side you can use the onload method on the body tag.
HTML
<body onload="SelectPage();"></body>


Or you can even use jQuery's document.ready() function
JavaScript
$(document).ready(function () {
   SelectPage();
});
 
Share this answer
 
Comments
eman88 2-Mar-12 12:11pm    
thank u for ur answer :)
You cannot, because the parameter of your function is not defined. You also need some code to calculate the value of DocURL, but this code cannot have any input parameters just because the onload handler does not have any parameters.

I suspect you need something different. This function can be called with some default parameter on load, and called from somewhere else, like menu handler, with all the parameters.

Another problem is hard-coded URLs. Do you know this principle, one of the most important in all practical programming: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^]?

You could would require typing the some URLs elsewhere, which is no less but a support nightmare. You need to centralize all UTLs and associated data and code (handlers, messages you show) in one single object, the way no value would be repeated. This is just one of possible approaches.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900