What I am trying to do is redirect a user based on their referring url to a promo page. In the script below if someone comes from a referring url from “mydomainsite.com” they will be sent to “mydomainsite.com/promo.html” When I have the script below in the page “mydomainsite.com/promo.html” and it comes from a refer of “mydomainsite.com” it seems to loop or continue to load the page and it never loads the page “mydomainsite.com/promo.html” The script has to be in the “mydomainsite.com/promo.html,” do to the page being a promo page and everyone can’t have access to the page. I assume this is due to indexOf and it checks for “mydomainsite.com” then executes. Is there away to fix this?
<script language="JavaScript">
if (document.referrer.indexOf('mydomainsite.com') > -1)
location.href='http://mydomainsite.com/promo.html';
else
location.href='http://notfrommydomainsite.com';
</script>
As has been mentioned in the comments the referrer data is not reliable, however if you do want to pursue this...
The mqost likely cause of the issue you have is that when arriving at the redirected promo.html it finds the referrer is from mydomainsite.com so it simply goes to itself creating am infinite loop.
You will need to test for that and avoid the loop by checking that location.pathname is not promo.html :
if (document.referrer.indexOf ('mydomainsite.com') > -1 &&
location.pathname !== 'promo.html')
location.href = 'http://mydomainsite.com/promo.html';
Related
I have add this code in the body tag. the web page keeps on refreshing like a loop.
<body onload="window.location.reload()">
I tried searching for a Javascript code or for a condition that will make it stop. I found a ways to do it but when I test it, they are not working.
I'm sorry but it is not possible to refresh the page 1 time once the page is completely loaded.
I want only once load page when i open the page. Can any one please tell me
You could do something like:
When the page loads, read the URL.
If there is no fragment identifier (e.g., #reloaded), add the fragment identifier and reload the page.
If there is a fragment identifier (e.g., #reloaded), don't reload.
This isn't necessarily a fool-proof technique, but might serve your purposes.
window.onload = function() {
if (window.location.hash !== "reloaded") {
window.location = window.location + "#reloaded";
window.location.reload();
}
}
I'm sort of new to JQuery, but I'm practicing everyday. My goal is to open the link after the buttons have been clicked but the link doesn't seem to be opening. I'm trying to open the link inside the if statement so everything happens accordingly.
window.setInterval(function(){
if ($('#add-remove-buttons').find('.button').length > 0) {
$('#size').val($('#size option').filter(function(ind, el) {
return $(el).text() === 'Large';
}).val());
$('#add-remove-buttons').find('.button').trigger('click');
setTimeout(function() {
window.location.replace('http://myweblink');
}, 900);
}
}, 100);
EDIT (STILL NEED HELP)
I've tried changing it but it doesn't load. I think it might be getting stuck in the 100ms loop. I put the function in a 100ms loop so it can detect if ($('#add-remove-buttons').find('.button').length > 0) I also just realized that after the user clicks the button, this html automatically appears:
<fieldset id="add-remove-buttons"><input class="button remove" name="commit" value="remove" type="submit">keep shopping</fieldset>
This means that the if statement : if ($('#add-remove-buttons').find('.button').length > 0) from my code, becomes false and the code for changing the URL doesn't run. Is there a way to detect the presence of the html code above like the if statement that became false? After I figure that out, I can put the window.location.href = "http://myweblink"; and then get it to work!
And in your code it is missing the complete web address.
Use
window.location.replace('http://myweblink.com');
Instead
window.location.replace('http://myweblink');
To redirect,jQuery is not necessary, and window.location.replace(...) will best simulate an HTTP redirect.
It is better than using window.location.href =, because replace() does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco. If you want to simulate someone clicking on a link, use location.href. If you want to simulate an HTTP redirect, use location.replace.
For example:
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
You can read the answer here.
Try
window.location.href = "http://your.wesite.com";
This will replace your address bar.
If you're using jQuery, just use $(location).attr('href',url);.
window.location.href seems to have inconstant behavior in some browsers, in fact, it flat out doesn't work in my version of Firefox.
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.
My url format is like :
http://domain.in/home
http://domain.in/books/notes
http://domain.in/books/notes/copy
I've called a javascript function on window.load to check if the url has changed or not.
If the url has been changed then code is executed else it will return and checks again after 5 sec.
My code is :
window.onload = function(){
setInterval(function(){
page_open();
}, 5000);
};
function page_open(){
var pages=unescape(location.href);
pages=pages.substr( pages.lastIndexOf("studysquare.in/") + 15 );
// gives book if url is http://studysquare.in/book
//alert("pages"+pages+"\n\n recent"+recent);
if (pages==recent) { return; }
recent=pages;
alert("Reached down now the code will execute.");
}
The problem now is : when the url is like :
http://domain.in/book
Single level deep, then everything works fine. But when the url is like
http://domain.in/book/copy or http://domain.in/book/copy/notes
Then nothing works.....
Any help to check 3 level deep url change in javascript every 5 sec ? :)
Hi sorry I forgot to tell that... I've .htaccess file which doesnt allow to navigate the page when any length url after the domain.in/ is written.... that means only single page remains open and not affected by the url change...
When the user changes the URL, the browser unloads the entire page they're currently on (including your javascript, hence it stops running) and then loads the next page. No javascript is able to run across page changes. You can't monitor a change in the URL like you're doing if they're navigating to another page.
The best way to catch a change in the URL is to add an onUnload event to the body object to fire your javascript when the browser unloads the page just before starting to load the new page the user has requested -- but I'm not sure that's going to help achieve your goal of tracking their recent page views (if that's what you're looking to do).
Sounds like a history plugin such as jQuery address would help you a lot.
It lets you handle the event when the URL is changed, so you can load in new content as required.
My page runs on php for no js-users. For users with javascript on I load all content dynamic fromt the index in combination with the hashchangevent. So the links all look like www.page.com/#page.php, with a # before it. If the user types it in that way every thing works like charm and the content is being loaded over the index.php
But if a user would enter www.page.com/page.php the page of course ends up on the php page and the dynamic page will of course not work any more except the user will hit the index page and go on navigate from that. So that's not a cool way.
My Question:
How can I redirect user from:
www.page.com/page.php
to
www.page.com/#page.php
when they typed in www.page.com/page.php in the Browser
Of course only with javascript. The Page should work without javascript on normally with php.
Thank you.
When the page loads, change the window.location to the desired URL by deconstructing and reassembling the URI. Unfortunately, you'll have to include this on every page (i.e., page.php) on which you'd like the redirect to happen.
$(document).ready(function () {
// Insert the pathname after the hash, but skip the leading '`'
window.location = "/#" + document.location.pathname.slice(1);
});
It's slightly less clean, but you could also remove the window.location bit from .ready() entirely. This would effect an small performance improvement.
If you want the hash-redirect to affect only the last element of the current path, use the code below, instead.
window.location = "./#" + document.location.pathname.split('/').pop();