Passing value to JS function through URL on window load

后端 未结 3 1265
春和景丽
春和景丽 2021-01-20 08:11

my page http://www.dinomuhic.com/2010/index.php loads the Showreel at the start of the page using an onLoad call in body like this:



        
3条回答
  •  天命终不由人
    2021-01-20 08:34

    if you are using jQuery, why not use the DOM-ready handler instead of onload ? on that note, if it's just an ajax request, you don't even need to wait for DOM-ready. if you parse the query, you should be able to pass that in to your existing function.

    // Wait for DOM-ready, may not be needed if you are just
    // making a request, up to you to decide :)
    $(function() {
        // window.location.search represents the query string for
        // current URL, including the leading '?', so strip it off.
        var query = window.location.search.replace(/^\?/, "");
    
        // If there is no query, default to '96'
        if ( !query ) {
            query = '96';
        }
    
        // Call existing function, passing the query value
        sndReq( query );
    });
    

    hope that helps! cheers.

提交回复
热议问题