Reputation: 15734
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?
Upvotes: 4
Views: 14362
Reputation: 17757
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;
Upvotes: 2
Reputation: 39532
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?
Upvotes: 6