mac007 Posted April 16, 2020 Share Posted April 16, 2020 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! :) Quote Link to comment https://forums.phpfreaks.com/topic/310601-need-url-parameter-read-so-it-gets-embedded-into-a-buy-link/ Share on other sites More sharing options...
requinix Posted April 17, 2020 Share Posted April 17, 2020 MDN has something useful. Quote Link to comment https://forums.phpfreaks.com/topic/310601-need-url-parameter-read-so-it-gets-embedded-into-a-buy-link/#findComment-1576958 Share on other sites More sharing options...
mac007 Posted April 17, 2020 Author Share Posted April 17, 2020 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> Quote Link to comment https://forums.phpfreaks.com/topic/310601-need-url-parameter-read-so-it-gets-embedded-into-a-buy-link/#findComment-1576960 Share on other sites More sharing options...
requinix Posted April 17, 2020 Share Posted April 17, 2020 37 minutes ago, mac007 said: Thanks requinix, I took a look at it and looks like it's what I need... But apparently you didn't read the second sentence on the page. Quote Link to comment https://forums.phpfreaks.com/topic/310601-need-url-parameter-read-so-it-gets-embedded-into-a-buy-link/#findComment-1576964 Share on other sites More sharing options...
mac007 Posted April 17, 2020 Author Share Posted April 17, 2020 Quote Ahh.. I see it's a bit different - gonna try that out tomorrow, Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/310601-need-url-parameter-read-so-it-gets-embedded-into-a-buy-link/#findComment-1576965 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.