1

I need to redirect page if div id found inside the page.

I have this script :

 <script>

 if (!document.getElementById("locationsHolder")) {
 window.location.href = "logged.html";
 }
 </script>

On the same page I have this

 <div class="locationsHolder" id="locationsHolder"></div>

Although I have everything right it loads logged.html page whatever id I put on getElementById. i.e. getElementById("notexist")

4

5 回答 5

3

You should do if (document.getElementById("locationsHolder")) since you want to do something if the div is found. Also you should put the script at the end of document or use jQuery $(document).ready() to make sure the entire page is loaded prior to running the script.

于 2013-03-13T21:23:09.903 回答
1

Maybe this will do what you need:

<!-- Put the following somewhere in the head block: -->
<script type="text/javascript">
window.onload = function(){
    if(document.getElementById("locationsHolder") == null){
        // Do something here if the ID is NOT found on the page
    }else{
        // Do something here if the ID IS found on the page
        document.location = "logged.html";
    }
};
</script>
于 2013-03-13T21:46:06.910 回答
0

you should ask if it is == null. try this in the developer tool.

!null --> true

!"" --> true

!document.getElementById("locationsHolder") is always true.

you should do something like

!document.getElementById("locationsHolder") == null

于 2013-03-13T21:24:14.437 回答
0

you probably tested for the existence of div id before even the page has been loaded. I would suggest you to either place the script after the div id or invoke the call only after the onload event.

window.onload=function(){
    // your code
}
于 2013-03-13T21:34:15.873 回答
0

It was the '!' that I remove it and script was before div id. Now it's working.

I need it this because I want home page redirected if another page contain a div id. I have another script inside index file that looks for page2 if contain div id , and it worked until I made this script from this topic that checks if that div is present on the page.

So, this script is one home page and checks if div id found on page2 and loads it on home page :

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">
jQuery(function($){
$('#result').load('Page2.html #intro');
});
</script>
</head>
<body>
<div id="result"></div> 

Code from page2:

<div id="result"><div id="intro">Hi, this is the Intro!</div></div>

Now the script from this topic can't find div id "intro" , and actually it dosen't load as bellow script search for 'intro'(and jquery above stops working), but if I search for other div from the page is working Must be a conflict.

  <script>

 if (document.getElementById("intro")) {

  window.location.href = "logged.html";
 }
 </script>

What is wrong? Maybe there is another more simple way to trigger redirection if page.2 contain div id "intro" ?

I tried to trigger redirect directly from page1 with this script but won't work:

<script type="text/javascript"   src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js">    </script>
<script type="text/javascript">
if ('page2.html #intro').length === 0){
window.location.href = "logged.html";
}
</script>
于 2013-03-14T07:22:43.127 回答