hi there,

I need help on replacing url parameter, I am using Zeroclipboard script on my page. When someone click on any coupon code then a new window open with the same page and code can be copied from popup. I have these codes on my page :

<div class="btn btn-primary" id="coupon_code_container_<?php echo $couponvalue['id'];?>" >
<strong code="<?php echo $couponvalue['id'];?>" id="coupon_code_<?php echo $couponvalue['id'];?>" data-clipboard-text="<?php echo $couponvalue['couponcode'];?>" class="copyCoupon">GET COUPON CODE</strong>

AND this java script :

<script type="text/javascript">
var join = location.href.match(/\?/)?'&':'?';
var newtab = window.open(location.href +join+'code='+this.getAttribute('code'),'_blank');
//var newtab = window.open(location.href +'#'+this.getAttribute('code'),'_blank');
window.location.href = "/opensite.php?store="+this.getAttribute('code');
var code = /code=(\d+)/.exec(window.location.search)[1];
if (code){
coupon_code = $('strong[code='+code+']').attr('data-clipboard-text');
$('#myModal #coupon_code').val(coupon_code);
$('#myModal .copy_coupon').attr('data-clipboard-text',coupon_code);
var client = new ZeroClipboard($('.copy_coupon'));
client.on( "aftercopy", function( event ) {
$('#myModal .copy_coupon').html('COUPON COPIED');

My page use pagination like : mysite.com/fasion.html?orderby=id&start=8

Now my problem :

When i go mysite.com/fasion.html and click any coupon code then it opens in new tab and URL of new window is mysite.com/fasion.html?code=123

BUT when i click any coupon on mysite.com/fasion.html?code=123, a new window opens the and now new window URL is now mysite.com/fasion.html?code=123&code=123 and so on....

Same happening if i do it on paginated page like mysite.com/fasion.html?orderby=id&start=8&code=123
and mysite.com/fasion.html?orderby=id&start=8&code=123&code=456 and so on...

What i want : code=123 should be replaced with new one. I can guess but can not figure it out here :

var join = location.href.match(/\?/)?'&':'?';
var newtab = window.open(location.href +join+'code='+this.getAttribute('code'),'_blank');
//var newtab = window.open(location.href +'#'+this.getAttribute('code'),'_blank'); 

problem can be seen here : catchcoupons dot in/category/Fashion-and-Accessories.html

3 Years
Discussion Span
Last Post by Taywin



Based on that, you could try this - apologies for spaghetti code, but very late here:


    function setQueryVariable(variable,varValue,fullURL)
           var query = window.location.search.substring(1).trim();
           var used = false;
           var qs = '';
           var locationNoQs = window.location.href.split('?')[0];
               var vars = query.split("&");  
               for (var i=0;i<vars.length;i++) {
                    var pair = vars[i].split("=");
                       if(pair[0] == variable){
                           vars[i] = variable + "=" + varValue;
                           used = true;
                    vars[i] = variable + "=" + varValue;   
               qs = vars.join("&");
                qs = variable + "=" + varValue;   

           return locationNoQs + '?' + qs;



Not foolproof. malformed urls would probably cause problems.


Depends... If you know the exact format, you could simply use replace() function.

var newLoc = window.location.href.replace(/\?code=\d+/i, "").replace(/\&code=\d+/i, "");

The above means the location with either ?code=#### or &code=#### will be removed from the URL. However, this is for exact format & variable that you know before hand. The different is its value and is only for number (as \d would match only number).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.