Website duplicates query string parameters in in-site links - javascript

I have an issue with IsaFYI.com when Google URL tracking code is used.
For example, if one goes to the page: http://isafyi.com/mom-of-two-covers-college-expenses-with-isagenix-business/then clicks on the recognition link at the bottom of the story, they will be taken to the page http://isafyi.com/category/recognition/. If one were to then click on the link for Sales Promotions, they would be brought to the page http://isafyi.com/sales-promotions/.
However, if tracking code is placed on the first page, (in this case, ?utm_source=test&utm_medium=test&utm_campaign=test,) the first url looks like this:
http://isafyi.com/mom-of-two-covers-college-expenses-with-isagenix-business/?utm_source=test&utm_medium=test&utm_campaign=test
If one clicks on this link, and now clicks on the recognition link at the bottom of the page, they are brought to
http://isafyi.com/recognition/?utm_source=test&utm_medium=test&utm_campaign=test?utm_source=test&utm_medium=test&utm_campaign=test
If you look carefully, you can see that the tracking code is now on the link twice:
http://isafyi.com/recognition/
?utm_source=test&utm_medium=test&utm_campaign=test
?utm_source=test&utm_medium=test&utm_campaign=test
If from this page one were then to click on the Sales Promotion link, they would be brought to:
http://isafyi.com/salespromotions/utm_source=test&utm_medium=test&utm_campaign=testutm_source=test&utm_medium=test&utm_campaigtestutm_source=test&utm_medium=test&utm_campaign=testutm_source=test&utm_medium=test&utm_campaign=test
The doubled code has been doubled again. This increases exponentially with each click on an internal link.
If a user now clicks on a link leading to an outside site, it is rejected by the server. The message that appears is:
The requested URL was rejected. Please consult with your administrator.
Your support ID is: XXXXXXXXXXXXXXXXXXXXX

You have a javascript file "isafy.js". There you have a piece of code:
var curUrl = window.location.href; // Gets the url of the current page
var qs = curUrl.slice(curUrl.indexOf('?utm_')); // Checks url for Google Analytics Query string and if it exists, it is stored in variable 'qs'
// If 'qs' has 1 character or less it is cleared out
if(qs.length <= 1){
qs = ""
}
// Goes through every a link on the page and appends the Google query string to every link
$('a').each(function() {
var href = $(this).attr('href');
href += qs;
$(this).attr('href', href);
});
This code adds the utm strings but does not check if the parameters have already been appended. So with every clicked link you get another instance of the parameters added to the url (so Crayons comment was spot-on).
I couldn't even tell what this is for - for Google Analytics it's enough if the parameter is set on the landing page. If you need the utm values somewhere else in the page you should consider using a cookie, if not drop this piece of script.

Related

I don't want the user to get the file directly from the url

For example:
url(`https://example.com/file.exe`)
If a user visits this website directly, the user will be redirected to https://example.com/getfile.html
but allow the user to get the file if the user gets the file from <a> tag of https://example.com/getfile.html
You can achieve this by removing href attribute in anchor tag and adding onclick function to the anchor tag. In onclick function send some parameter in url to get that value in your html page when user visits the page.
Click here to know how to send parameter in url.
Click here to know how to get post parameter from the url .
Still you have any confusion, you can comment your questions to this answer.
If you are using php you can get post parameter from url in a single line of code by using $_REQUEST["your post parameter name"];
I am adding a simple example for this to achieve with simple html and jquery.
with jquery document.referrer method on document ready(when user opens a page), you can get the previous url of user visited. like bellow example. for this you need to include jquery in your page by following line inserting in your page.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
console.log(document.referrer);
if(document.referrer == "your url") {
your code to allow user if comes from the url you mentioned //user will get download button or you can write direct download option;
} else {
window.location = "some url"; //you can redirect user to specific url
}
});
<script>

Is it possible to automatically open a video modal and begin playing only when the user accesses the page from a link in an email.?

I know that we can open Bootstrap modal on page load very easily. But I have a situation where the modal should open ONLY when the user accesses the page from an email link?
Is there any way to do using JavaScript or JQuery.
Yes, you can.
The most straightforward way I can think of is to use an extra query parameter in the url used in the email.
When your page loads, you can test for this query parameter and then decide to open your modal or not.
Consider this sample URL:
www.example.com
In the email version, we can add a query parameter like this :
www.example.com?email=true
and then, on your client side (on page load):
var query = window.location.href.split('?')[1];
if(query && query.indexOf('email=true') > -1){
// Url has the parameter run code to open modal
}
Yes you can do this
Just you have to pass extra parameter ( querystring) in the Link.
Suppose your Normal link is "www.xyz.com", instead of this you have to pass "www.xyx.com?bylink"
and on your page load you have to get the page Url & find the extra parameter (querystring)
If the parameter is there in the URL just open the Video Popup or else Open the page normally .
Eg:
var URL =window.location.href ;
if(URL.contains('bylink'))
{
videoModel.show();
}
else{
NoramlPage.show();
}

javascript:history.back() OR homepage

I would like to add a button that will take a user back a page OR if a user used direct url to get to that page (so, .back() would take them back to google for example) - a user will be redirected to my homepage.
Looks like javascript:history:back() or history.go accept step attributes only (how many pages we can go back) and that is it. Will I have to tap into session or is there a way to do it with history object?
You can check the referrer url with document.referrer, to see what was the previous page where the user come from. It can be empty that means user come from direct link, or clicked the link from other app links skype etc...
In the example I used https://stackoverflow.com as an example, but you can make it more universal if you use with location.origin more info
As #Jonas W. Mentioned in the comments, be sure you check the full domain in the right position in the referrer, because of security reasons.
function go() {
if (document.referrer.indexOf("https://stackoverflow.com/") === 0) {
history.back();
} else {
window.location.href = "https://stackoverflow.com";
}
}
document.querySelector('button').addEventListener('click', go);
<button>back</button>

Javascript - Get URL being navigated to

Is there any way to use JavaScript's OnUnload() function to find out which URL the user is navigating away to?
For example, if my code is in page1.html, and the user clicks a link to http://example.com, is there any way for JavaScript code present in page1.html, to retrieve the URL "http://example.com" and display/store it before the page unloads?
I am able to do this if I invoke a function through my link by using its OnClick, but I cannot find a way to do this otherwise. (I can post my code for that if needed, but it does meet my business requirement)
EDIT : This looks to be impossible, since my business requirement demands that I do not make any change to the content of the page, excepting the adding in of a javascript file where this code is present.
Ignore onBeforeUnload/onUnload, you don't need that. You can do it with a simple click handler like this:
$('a').on('click', function(e)
{
e.preventDefault();
var destinationLink = $(this).attr('href');
$.post('/your/analytics/url', {link:destinationLink}, function()
{
// Success
window.location.href = destinationLink;
});
});
This will stop any link from working until it's been submitted to your analytics so it's not ideal - you need to make sure what ever is receiving the data does so as quickly as possible.
You could replace the current url of the clicked link.
That will allow you to call your server to do the check of the clicked url, and then redirect it.
The code bellow change the url of the clicked link only for a couple of microseconds
$("a").on("click",function(e){
// Save the current link
var h = this.href;
//Change the link of the current a
this.href = "http://www.example1.com/redirect.php?url="+encodeURI(h);
// replace the href with the original value on the next stack
setTimeout((function(my_link){
return function(){
my_link.href = h;
};
})(this),0);
});
my link

remember most recent click

So I want to be able to have a different styling for a link after you go to the page it's clicked on. I have been using the following code:
$(document).ready(function(){
var url = document.URL;
function contains(search, find) {
return search.indexOf(find) !== -1;
};
$('#topbar a').each(function(){
var link = $(this).attr('href');
var answer = contains(link,url);
if(answer === true){
$(this).addClass('check');
}
else{
$(this).addClass('nocheck');
};
});
});
This goes through the links in my navigation bar and checks if it's on the same page as the link, and it works, but I can't use it in one specific case: Random.
I have a link that generates a random page from the pages I have, so it does not have a specified link as it links to a function to randomly generate the page (note: I cannot change the function or access information from it).
So how can I detect that the random link was clicked previously so i can give it the .check class
If i understand your question correctly, your function does not work for the randomlink because this has a href like http://mysite.com/random, but the server will actualy redirect you to a different page, like http://mysite.com/about-me, and therefore the url of the active page does not match the href of the random button, and it will not get the active state.
One could argue if you would want it to get the active state, cause clicking it again would not (likely) bring you to the same page, but that is besides the question.
I can see to ways to solve this.
server side:
In stead of redirecting to ie. http://mysite.com/about-me in the random function, you could also redirect to http://mysite.com/about-me?random. By adding this get variable, you should not change the behaviour of the link (unless you have some very strict controller, or that variable is actually used, but that is unlikely). You could then detect with javascript if that variable is present in the url, and then activate the random button.
Something like this:
$(document).ready(function(){
var url = document.URL;
// check for random
if (url.indexOf('?random') >= 0) {
$('#topbar a.random').addClass('check');
}
// check all other
$('#topbar a:not(.random)').each(function(){
if($(this).attr('href').indexOf(url) >= 0){
$(this).addClass('check');
}
else{
$(this).addClass('nocheck');
};
});
});
cookie:
If you do not have acces to the server side random controller, you could do it entirely with javascript, by the use of a cookie (the only way I know to make a variable persist trough page requests).
On click of the random button, you would first set a random cookie to true with javascript, before letting the actual link do it's thing. On entering the page, you could then do a similar check as in my previous option, but in stead of the url you check if the cookie is tre. If so, you change it to false (so on the next page request the random button will not be active again) and set the randombutton to active.
As I believe the first solution is to be preferred (cookies should only be used as a last resort, they are sent on every page request, which means extra data, and your user might have cookies disabled, or there might be laws against using cookies, so the function could not always work), I will not write the javascript yet. Feel free to ask if you prefer this solution and need further help however.

Categories