4

I am trying to write a function like this:

if ( document.url!= 'search.php') {
    window.location = 'search.php' 
}

This is not working; it seems I may need to check if URL string contains 'search.php' - or is there another way I can do this check?

0

2 Answers 2

6

It sounds like you're searching for indexOf. Note that it's not document.url, but document.URL:

if (document.URL.indexOf('search.php') == -1) {
    window.location = 'search.php';
}

Note: This will also match the following:

http://www.domain.com/search.php
http://www.domain.com/index.php?page=search.php&test
http://www.domain.com/search.php/non-search.php
http://www.domain.com/non-search.php#search.php

Extra note: Why are you doing this? If people have javascript disabled, they will never get forwarded. Maybe you're searching for the 301 or 302 header?

Sign up to request clarification or add additional context in comments.

5 Comments

That is what I wanted, but you have a good point; when users click on a search input field, which is persistent in the header, I want them to automatically be forward to a search page where there is a AJAX post that displays search suggestions as they type. A better way of doing this then?
@KickingLettuce You could heavily style an <a> tag or a <button> to look like your search field, on all the pages that aren't search.php, so when you click, the browser would just act like it was any other <a> tag. Personally I think you should have a dynamic search input box on all pages instead, though.
I do technically have the search box on all pages.
@KickingLettuce But you shouldn't forward users who click it - why not just make it dynamic right there, like most websites? (Facebook for instance).
A drop down box would be ideal yes. I dont know how to do that yet, but good reason to learn. Currently, the Search Suggestions simply use up the main <div> of the body of the page. Ok thanks for your help..
2

I always use this.

if (window.location.href != 'search.php') {//get the current url
        window.location.href = 'search.php' //redirect
    }

Try any of these

- alert(document.URL)

- alert(Window.location.href)

- alert(document.location.href)

- alert(window.location.pathname)

for creating dropdown with links -:Put this in the success part of your ajax

            var data=JSON.parse(xmlhttp.responseText);
            var alldata;
            for(var  i=0;i< data.length;i++)
            {
                alldata += "<a id='some_id' onclick='myFunction(this.id);' href='whatever#' class='link'>"+data[i][0]+"</a><hr>";
            }
            //data[i][0]-->this depends on your json text.
       document.getElementById('your_input_field_id or div_id').innerHTML = alldata;

1 Comment

KickingLettuce:check ,i have done the same thing with autocomplete...might help you

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.