1

I do not want to use code behind to do the redirect with query string, because will using the wysiwug editor for do this. after the submit, would like it to redirect to http://abc.somedomainname.com?first=var1&last=var2

name

expert please advice how am i able to redirect with querystring with javascript or jquery. please write the detail because i totally new in this.

1 Answer 1

0

Step 1: Put two methods in the <HEAD></HEAD>:

<script type="text/javascript" language="javascript">
// return the value of a parameter based on the name you pass in
function getParameterByName(name) { 
    // use regular expression to replace [ with \[, and replace
    //   ] with \], you get this if you
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");

    // make a new regex that strips out the querystring parameters
    // the [\\?&] looks for the beginning ? or & characters,
    // then the name of the parameter (you passed it in to the method)
    // then the "=([^&#]*)" is looking for = followed by any character
    // except & or #. Because there are parentheses around this part
    // it will remember the match in a match group.
    var regexS = "[\\?&]" + name + "=([^&#]*)"; 

    // this converts the regular expression string into a RegExp object
    var regex = new RegExp(regexS); 

    // execute the regular expression against the URL
    var results = regex.exec(window.location.search); 
    if(results == null) {
        // it didn't find the parameter you passed in
        return ""; 
    } else {
       // we got a match! The value of your parameter is in the 
       // results array (parentheses in the regexS above told the
       // regex to store it there. We do a quick replace at the end
       // to convert the + character to a space, because it is URLEncoded
       return decodeURIComponent(results[1].replace(/\+/g, " ")); 
    }
}

// call this method to do the redirect
function redirectBasedOnParameters() {
    // read the value of the parameter named 'first'
    var var1 = getParameterByName('first');

    // read the value of the parameter named 'last'
    var var2 = getParameterByName('last');

    // redirect the browser
    window.location = "http://abc.somedomainname.com?first=" + var1 + "&last="+ var2

}
</script>

Step 2: Use the Body.onLoad event to force your JavaScript to fire

Once you have your script inside your web page's <head></head> tags (see below). Then you can force the script to execute when the body loads like this:

<html>
<head>
<script type="text/javascript">
  // put the JavaScript code mentioned above inside here 
</script>
</head>

<body onload="redirectBasedOnParameters()">
    <h1>Hello World!</h1>
</body>
</html> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.