1

これを行うためにwysiwugエディターを使用するため、クエリ文字列でリダイレクトを行うためにコードビハインドを使用したくありません。送信後、 http://abc.somedomainname.com?first=var1&last=var2にリダイレクトしてほしい

名前

専門家は、javascriptまたはjqueryを使用してクエリ文字列でリダイレクトする方法を教えてください。私はこれでまったく新しいので詳細を書いてください。

4

1 に答える 1

0

ステップ 1: に 2 つのメソッドを入れます<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>

ステップ 2:Body.onLoadイベントを使用して JavaScript を強制的に起動する

Web ページのタグ内にスクリプトを配置したら<head></head>(以下を参照)。次に、本体が次のようにロードされたときにスクリプトを強制的に実行できます。

<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> 
于 2012-04-26T00:17:06.847 に答える