English 中文(简体)
根据文档位置从 Js 文件重定向
原标题:Redirect from Js file depending on document location
I am using code below for for my js file for redirecting.. but i want my redirection based on top location. for example if someone visit xyz.com so it redirect to mydomain.com so what code i needed to add? i think it could be like indexOf( xyz.com ) loadScript("http://j.maxmind.com/app/geoip.js", function() { var country = geoip_country_code(); if (country === "US") { window.location = "http://mydomain.com/"; } }); function loadScript(url, callback) { // adding the script tag to the head as suggested before var head = document.getElementsByTagName( head )[0]; var script = document.createElement( script ); script.type = text/javascript ; script.src = url; // then bind the event to the callback function // there are several events for cross browser compatibility script.onreadystatechange = callback; script.onload = callback; // fire the loading head.appendChild(script); }
问题回答
I am not totally sure I understand your question, window.location is an object which refers to the currently displayed web page at all times. As you already know you can change window.location and cause a page change in the browser. window.location also has several properties which help in analyzing its contents, see the referenced link for more details. window.location.host is the one you need I think. So if I understand your question correctly, you could replace var country = geoip_country_code(); if (country === "US") { window.location = "http://mydomain.com/"; } with if (window.location.host === xyz.com ) { window.location = "http://mydomain.com/"; }




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签