Jump to content

Need url parameter "read" so it gets embedded into a buy-link


Recommended Posts

 

Hi, I need a JAVASCRIPT snippet that can "read" a specific url parameter going to a cart, and embeds it into a link and if parameter is not present then use a default value, so say something like this...

This would be url-link: www.shoppingcart.com?source-code=XYZ&date=whatever&name=whatever

So, when one arrives at that page, there is a buy-link, and I need javascript to embed the "source-code" url-parameter into the link like this:

<a href="http://www.shoppingcart.com?add-item=shoes&source-code=XYZ"> Buy! </a>

So, as you see, the "source-code" parameter gets automatically inserted into the buy-link,

I've done this sort of stuff rather easily thru PHP like this:

https://shoppingcart.com?add-item=shoes&source-code=<?php if(isset($_GET['source-code'])) {echo $_GET['source-code'];} else { echo "DEFAULTCODE”;} ?>

But I havent been able to get it working just right in javascript, does anyone have any ideas best way to accomplish this?

Appreciate any help!  :)

 

 

Thanks requinix, I took a look at it and looks like it's what I need... I found this script that uses that - I tried to get the variable to show up, but somehow havent been able to do it - this is what I have, assuming the URL has a "post=xyz" parameter in it. I'm very green with JS, so havent been able to figure out what I'm missing,

Thanks for your help!

<script>
function getUrlParameter(name) {
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    var results = regex.exec(location.search);
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
</script>


<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML.getUrlParameter('post'); 
</script>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.