Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6778765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:18:17+00:00 2026-05-26T16:18:17+00:00

If the current page URL has an argument ‘myid1’ in the querystring, for each

  • 0

If the current page URL has an argument ‘myid1’ in the querystring, for each link in my webpage with class ‘rewrite’, I want the link href’s querystring to be replaced by the current page URL’s querystring.

I’m using the following code:

<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>

<script type="text/javascript">
            $(function() {
                var requestid = new String(gup('myid1'));
                if (requestid!=null&&requestid!="") {
                $("a.rewrite").each(function() {
                    var href = $(this).attr("href");
                    href += "?myid1=" + requestid;
                    $(this).attr("href", href);
                 })
               }
            })
            //gup taken from here:http://www.netlobo.com/url_query_string_javascript.html
            function gup(name) {
                name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
                var regexS = "[\\?&]" + name + "=([^&#]*)";
                var regex = new RegExp(regexS);
                var results = regex.exec(window.location.href);
                if (results == null)
                    return "";
                else
                    return results[1];
            }
</script>

<a href="http://www.otherdomain.com?someid=1234" class="rewrite">Hyperlink</a>

The problem is that the URS’s querystring is being added to the links without removing the existing one. How to fix it?

Also, how do I allow one more parameter called ‘myid2’. Thank you in advance for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:18:18+00:00Added an answer on May 26, 2026 at 4:18 pm

    You can remove a prefious query string with this:

    $(function() {
        var requestid = gup('myid1');
        if (requestid) {
            $("a.rewrite").each(function() {
                var base = this.href;
                var pos = base.indexOf("?");
                if (pos != -1) {
                    base = base.substr(0, pos);
                }
                this.href = base + "?myid1=" + requestid;
            })
        }
    })
    

    Several things to note in this revision:

    1. if (requestid) tests for null, undefined and "" so you can use just the one test.
    2. There is no need to use a jQuery object to access the href attribute. It’s faster and more direct to just use this.href.

    If you like fewer lines of code (though not quite as fast), you would do it this way:

    $(function() {
        var requestid = gup('myid1');
        if (requestid) {
            $("a.rewrite").each(function() {
                this.href = this.href.replace(/\?.*$/, "") + "?myid1=" + requestid;
            })
        }
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If the current page URL has an argument 'myid1' or 'myid2' in the querystring,
When using the Url view helper to build links, if the current page has
I want to take the current page's URL (using page:env(caller)) and extract a section
Using rails 2. I want a link to the current page (whatever it is)
Currently every page in my current location has a web-url as follows. /test/test In
How can you modify the URL for the current page that gets passed to
If my current url is www.example.com/thank-you, how do I echo out the page name
Using Php I would like to extract the current Url of a page including
I reference the current page in a class with Page page = HttpContext.Current.CurrentHandler as
I want to use javascript to insert some elements into the current page. Such

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.