I'm trying to redirect all my site mobile traffic to one page.
so I added redirect code in header.
<script type="text/javascript">
<!-- if (screen.width <= 699)
{ document.location = "/page/mobile/";
}
//-->
</script>
The problem is that after redirecting script continuous executing because header is the same on the whole site. so it's refreshing the page over and over again.
I want javascript to check for the /page/mobile/ in url and if it's there, do not execute redirect.
How can I achieve this? Thanks!
Do this.
if ( window.location.pathname !== "/page/mobile"){
window.location.href = "https://[your-host-name]/page/mobile";
}
So you can use window.location.pathname to access your path after host (and port)
https://developer.mozilla.org/en-US/docs/Web/API/Window/location
check window location in your if condition
if (window.location.href.indexOf("/mobile/")>-1){
console.log("You are on mobile page")
}
Related
For some reason, document.location.href , window.location.href , and window.location.replace is not working, I'm using localhost for now and I don't know how to redirect it to another webpage. I tried adding http:// on front, it's still not working, I tried making it redirect to another online website, It also didn't work. I read a lot of thread already and tried them but none of them worked for me.
var loginBut = document.getElementById("RDbtn1");
loginBut.addEventListener("click", checklogin);
function check(){
document.location.href = "http://localhost/some/directory/here";
alert(document.location.origin + '/some/directory'); // I just use this to know that the button is being pressed.
}
html:
<!-- some code here-->
<li><button class="login1" id="RDbtn1">LOGIN</button></li>
<!-- some code here -->
I believe you should redirect to particular html page and should give the port on which your local host application is working
document.location.href = "http://localhost:8080/some/directory/here/file.html";
It is window.location (nothing or replace or href) - document.location is normally used to get the actual page
What's the difference between window.location and document.location in JavaScript?
If you are on the same server, no need to qualify the page but I would add the actual page you try to load
window.location = "/some/directory/here/index.html"
<iframe src="http://runebet.com/" width="80%" height="65%" name="runeBetAPI"></iframe>
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<script>
function RegisterPage() {
window.frames["runeBetAPI"].location = "http://runebet.com/register/";
}
RegisterPage();
</script>
This doesn't seem to work/change the content..
The URL should start with http://www. to avoid a server redirection
you can access iframe contents from the same domain only read here.
You can use the following to change the source of the iframe.
function changeSource(src, frameID) {
var frame= $('#' + frameID);
if ( frame.length ) {
frame.attr('src',src);
return true;
}
return false;
}
But apparently what looks like looking into what your code is doing the url that you are redirecting to it actually opens a registration form inside modal window, which for some reason is not opening if you access it via an iframe, because if you put the url http://runebet.com/register/ directly in the iframe and load the page it will still be showing the landing page so it is redirecting but the modal is not popping up that leaves you with the last thing that you have to trigger the Register link click and that can only happen if you are in the same domain if not maybe someone else knows, i dont.
$(window.frames[0].frameElement.contentDocument).find('a[href="/register"]').trigger('click');
I've just created a mobile version for my site and I want to redirect users to it. so far i've added the lines of code below to redirect users from the main index page to the mobile index page:
<script type="text/javascript">
<!--
if (screen.width <= 800) {
window.location = "mobile.site.name";
}
//-->
</script>
This works, however how do I redirect users from let's say site.name/contact.html to to m.site.com/contact.html ?
Can you please help ?
I would highly recommend ASKING your user if they want to get redirected as opposed to just redirecting them.
You should Do this stuff in your .htaccess-file. Mod_rewrite is the solution. Have a look.
i want to redirect all traffic going to my Facebook app tab on my server directly to my Facebook app. Therefore I check with the following code if the user is inside the Facebook iframe or on my webpage:
<!-- language: lang-js -->
function referrerIsFacebookApp() {
if(document.referrer) {
return document.referrer.indexOf("facebook.com") !== -1;
}
return false;
}
if (!referrerIsFacebookApp()) {
top.location.replace("https://www.facebook.com/bommelME/app_264697733636385");
};
If I open up the page with the browser everything works as it should. But if I link to this page and open the link the redirect doesnt work. Any hints?
Use window.top to detect whether your app is in an iFrame or not. Try this.
if (window!=window.top) {
/* I'm in a frame! */
window.location = "https://www.facebook.com/bommelME/app_264697733636385";
}
Cheers.
I think you should check the window.parent object instead of document.referrer, because the page can be referenced from another one as you've said but not being included via iframe
I've an old website, navigation in an frame at left, pages at right.
I want when an page is url'd directly the nav (left frame) shows also.
Until now I was an js working, but I don't know from when it are not working,
now returns this message:
Forbidden
You don't have permission to access /master.html on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache/2.2.10 (Unix) mod_ssl/2.2.10 OpenSSL/0.9.8i DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Server at www.cpis.es Port 80
The code I was using is
http://www.webmasterworld.com/forum21/1205.htm
Call this javascript code from the HEAD section of each child page. The code creates a variable from the URL of the page, and then passes that variable in the new location's URL. This means a "master" frameset can load this exact page in the content section:
<SCRIPT LANGUAGE="JavaScript">
passpage = document.URL
if (top.location == self.location)
top.location.href="master.html?" + passpage
</script>
Then create just one "master.html" page. It holds the JavaScript code to decipher whatever URL is passed after the "?" and it writes that page into the content frame:
<html>
<head>
<title>Master Frameset</title>
</head>
<script language="JavaScript" type="text/javascript">
origURL = parent.document.URL
contentURL = origURL.substring(origURL.indexOf('?')+1, origURL.length)
document.write('<frameset cols="20%,80%"><frame src="leftnav.html" name="nav"><frame src="' + contentURL + '" name="content"><\/frameset>')
</script>
</html>
Thanks, Guillermo.
Sorry for delay.
Looks like the problem is in this little peace of javascript:
passpage = document.URL
if (top.location == self.location)
top.location.href="master.html?" + passpage
It should be:
passpage = window.location.pathname;
if (top.location == self.location) {
top.location.href="master.html?" + passpage;
}
You have to change that code in each page which you have in the link list. I think that should fix the problem.
I just checked the website and it seems to be working now. My guess is that there was no file located at http://www.cpis.es/master.html on server.
If the problem still exists please provide steps so we can reproduce it and see what went wrong.
Thanks Maiku Mori,
in order to properly test the problem please do the next:
go to www.cpis.es
click the second option in the menu (CoPrint)
ok, you see the leftnav + the page
now go directly to the page
go to www.cpis.es/coprint.htm
error, you got the forbidden page.