2

Having some difficulty getting this code to work. Essentially, I want to check if the referring url is coming from the /mobile directory and if not and the screen is a mobile device I want to redirect to the mobile site.

<script type="text/javascript">
if(window.location.href.indexOf("document.write(document.referrer)") > -1 &&
    screen.width <= 699) {
    document.location = "/mobile/mobile_home.asp";
}

</script>

The code is currently placed in the head of the main home.asp.

3 Answers 3

6

try this

if(document.referrer.indexOf('/mobile') > -1 && screen.width < 700) {
}
Sign up to request clarification or add additional context in comments.

Comments

3

You don't want document.write here (or virtually anywhere):

if(window.location.href.indexOf(document.referrer) > -1 &&
    screen.width <= 699) {

But your code and your question don't quite match, you've said

...if the referring url is coming from the /mobile directory...

That would be something more like

if(document.referrer.indexOf("/mobile") > -1 &&
    screen.width <= 699) {

Comments

2

Specifically addressing the indexOf check, 'does not equal' will also work and IMHO makes the intent a bit clearer.

if ( a.indexOf(somestring) !== -1 ) {

Applied to an example above

if(document.referrer.indexOf('/mobile') !== -1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.