Forum Moderators: open

Message Too Old, No Replies

Need help on replacing url parameter

url parameter

         

neo2sxn

2:59 pm on Jan 1, 2015 (gmt 0)

10+ Year Member



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

lucy24

4:49 pm on Jan 1, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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


I like to start by translating things into English. This one says:
FIRST get the existing query string
THEN make a new URL consisting of: current path PLUS existing query string PLUS "code=some-value-here".
This will always result in duplication of the "code=blahblah" parameter.

There are a couple of ways to achieve what you want. The two basic approaches-- still in English-- are:
-- get all parameter values, as well as the new value for "code", discard the existing query string and build a new one using the updated values
-- in the original query string (your variable "join"), test for the presence of "code=some-value-here". If present, replace it with "code=123"; if not, add "code=123".

If "code" always has a numeric value, the pattern is
\d*
where I'd use * instead of + just to protect against the parameter being defined-but-empty.

neo2sxn

5:00 pm on Jan 1, 2015 (gmt 0)

10+ Year Member



variable "join" is to check if its a paginated page. For example :
if page url is mysite.com/somepage.html
Then add ?code=123 OR if page url is mysite.com/somepage.html?orderby=id&start=8 then add &code=123...

I will be very thankfull to you, I am weak in java script, if you may create a solution example code.