Home > Technical & Creative Skills > Programming, Servers & Scripts

Dynamic text based on URL parameter (8)


12-30-2020 10:27 PM #1 bhal07 (Member)
Dynamic text based on URL parameter

I am trying to figure out how to get certain text on my landing page to change based on the URL parameter. So to give context, i am trying to show that a discount is either available or not available and this will depend on the users region which will be in the URL parameter of my landing page. So I was trying to get it to just show "Discount not available" if the URL parameter "m1" is Quebec for example. Anyways hopefully this is clear, I will now show you the code I came up with so far but I can't figure out why it's not working.

<script>
var province = getURLParameter("m1");
var province1 = province.toString();


if ( province1 = “Quebec” || "Newfoundland and Labrador" || "Northwest Territories" || "Nunavut" || "Yukon" ) {
document.write('Discount is no longer available... finding next best price...');
}
else {
document.write('Success! 50% Discount Available!');
}


function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1] || ''
);
}
</script>


So that's what I have so far and the idea is to show the discount is not available if they live in the provinces listed in that if statement. My question is, does anyone know how I could get this idea to work?


12-30-2020 10:37 PM #2 twinaxe (Senior Moderator)

Let me tag @jeremie, he probably knows what to do


12-31-2020 02:57 AM #3 jeremie (Moderator)

Your idea is close actually, but you need to repeat the province1 == for each test, like
province1 == "abc" || province1 == "def" || province1 == ...

Or you can do an array with the provinces and use include

Code:
provinces = [ “Quebec”, "Newfoundland and Labrador", "Northwest Territories", "Nunavut", "Yukon"];

if (provinces.includes(province1)) {
See

https://developer.mozilla.org/fr/doc...Array/includes


12-31-2020 03:51 PM #4 bhal07 (Member)

Quote Originally Posted by jeremie View Post
Your idea is close actually, but you need to repeat the province1 == for each test, like
province1 == "abc" || province1 == "def" || province1 == ...

Or you can do an array with the provinces and use include

Code:
provinces = [ “Quebec”, "Newfoundland and Labrador", "Northwest Territories", "Nunavut", "Yukon"];

if (provinces.includes(province1)) {
See

https://developer.mozilla.org/fr/doc...Array/includes
Amazing, thanks man. So I fixed up the if statement so the code is now...

<script>
var province = getURLParameter("m1");
var province1 = province.toString();


if ( province1 = “Quebec” || province1 = "Newfoundland and Labrador" || province1 = "Northwest Territories" || province1 = "Nunavut" || province1 = "Yukon" ) {
document.write("Discount is no longer available... finding next best price...");
}
else {
document.write("Success! 50% Discount Available!");
}


function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1] || ''
);
}
</script>

Thing is, it still isn't working. The weird part is that if I just do <script>document.write("Success! 50% Discount Available!");</script> it works perfectly fine, throw it in an if statement and suddenly it doesn't seem to work anymore.. So yeah i am very confused why this isn't working haha....


12-31-2020 03:57 PM #5 plutus (Member)

Quote Originally Posted by bhal07 View Post
Amazing, thanks man. So I fixed up the if statement so the code is now...

<script>
var province = getURLParameter("m1");
var province1 = province.toString();


if ( province1 = “Quebec” || province1 = "Newfoundland and Labrador" || province1 = "Northwest Territories" || province1 = "Nunavut" || province1 = "Yukon" ) {
document.write("Discount is no longer available... finding next best price...");
}
else {
document.write("Success! 50% Discount Available!");
}


function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1] || ''
);
}
</script>

Thing is, it still isn't working. The weird part is that if I just do <script>document.write("Success! 50% Discount Available!");</script> it works perfectly fine, throw it in an if statement and suddenly it doesn't seem to work anymore.. So yeah i am very confused why this isn't working haha....
replace your if statement with the one below:
Code:
if ( province1 === "Quebec" || province1 === "Newfoundland and Labrador" || province1 === "Northwest Territories" || province1 === "Nunavut" || province1 === "Yukon" )


12-31-2020 04:06 PM #6 bhal07 (Member)

Quote Originally Posted by plutus View Post
replace your if statement with the one below:
Code:
if ( province1 === "Quebec" || province1 === "Newfoundland and Labrador" || province1 === "Northwest Territories" || province1 === "Nunavut" || province1 === "Yukon" )
Okay thanks for that made that change too. Looks like it is still not going through the if statement for some reason. Still can't get this script to actually write out the messages.


12-31-2020 04:47 PM #7 plutus (Member)

Quote Originally Posted by bhal07 View Post
Okay thanks for that made that change too. Looks like it is still not going through the if statement for some reason. Still can't get this script to actually write out the messages.
second thing I see is that you call getURLParameter before declaration.

move function getURLParameter(name){ ... } to the top, right after <script> tag.


12-31-2020 05:26 PM #8 jeremie (Moderator)

Quote Originally Posted by plutus View Post
second thing I see is that you call getURLParameter before declaration.
I though that too, but when testing, it works with the function declaration at the end.

The problem is in the type of double quotes you use around the provinces' names. Some of them are not recognized. Make sure they are all standard double quotes, or use single quotes.

“Quebec” is different of "Quebec"


Home > Technical & Creative Skills > Programming, Servers & Scripts