I want refresh page using javascript and go to div id.
Example what I have done is
echo "<script type='text/javascript'>开发者_C百科top.location.href = '" . $url . "/page.php#contact';</script>";
Example the #contact
div is on half of page and it's not working, unless I hit the enter button and it goes to the #contact
.
<div id="contact">Contact</div>
If with php can be done
header('Location:/page.php#contact');
Let me know the correct way to do this
Technically your script should work. At least when I tried it, it seemed to work without too many problems. However, remember that if you're just putting the javascript on the page like that, then the browser may run it immediately without waiting for the entire page to load. My point being, that if you output the javascript before you actually place the DIV tag on the page, the browser may not see the div tag yet and can not scroll the browser there.
You want to either make sure that the script is outputted after the DIV in the HTML souce or that the javascript is run after the page has loaded by using something like this for example:
echo "<script type='text/javascript'>
window.onload = function () { top.location.href = '" . $url . "/page.php#contact'; };
</script>";
Also, if you want to refresh the page, then this will not work, because the browser assumes you just want to go the location of #contact
if the url is the same as the current url.
Simple:
location.href = '#contact';
Make sure your page has the bookmark or anchor defined near the contact div
in order to move on to #contact div, you must have anchor [contact] or bookmark defined there.
contact
then it should redirect to that respective location
echo $url; //for debugging purpose only, to see if you are writing this right!
echo "<script type='text/javascript'>
location.href = '" . $url . "/page.php#contact';
</script>";
No top
needed before location.href
.
精彩评论