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>
</div> 

AND this java script :

<script type="text/javascript">
$('strong').click(function(){
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');
newtab.focus();
});
var code = /code=(\d+)/.exec(window.location.search)[1];
if (code){
coupon_code = $('strong[code='+code+']').attr('data-clipboard-text');
$('strong[code='+code+']').html(coupon_code);
$('#myModal').modal('show');
$('#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');
});
}
</script> 

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

Recommended Answers

All 2 Replies

Member Avatar for diafol

From:

http://css-tricks.com/snippets/javascript/get-url-variables/

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

<script>

    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];
           if(query.length)
           {
               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;
                       }
               }   
               if(!used)
               {
                    vars[i] = variable + "=" + varValue;   
               }
               qs = vars.join("&");
           }else{
                qs = variable + "=" + varValue;   
           }

           return locationNoQs + '?' + qs;
    }

    alert(setQueryVariable("code",345));

</script>

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.