Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to another page without loading current page in jquery

I want to redirect to another page from 1 page without showing contents of that page using javascript/jquery.

So for example I would be either typing or coming from a search engine to a page on my website say www.mysite.com/aaa/ and I should get redirected to www.mysite.com/bbb/ without showing the contents of www.mysite.com/aaa/.

The server side is asp.net and I can do this using Response.Redirect but I do not want a code change.

From my limited knowledge, I cannot use document.ready or window.load as both will load the contents of the page in the browser before redirecting.

I am not aware of any other thing which would help me achieve this. Tried hard searching but could not get anything useful.

I got something here. I can have this in the header but right at the top of the header might not be possible. Plus the answer is not looking very convincing. However, can try it out and update this question with the findings.

Please help!

Thanks in advance!

like image 553
samar Avatar asked Oct 21 '15 07:10

samar


People also ask

How do I redirect a particular section of a page using HTML or jQuery?

One can use the anchor tag to redirect to a particular section on the same page. You need to add ” id attribute” to the section you want to show and use the same id in href attribute with “#” in the anchor tag.

Which method is used to redirect user from one page to another?

Approach: To redirect from an HTML page to another page, you can use the <meta> tag by specifying the particular link in the URL attribute. It is the client-side redirection, the browsers request the server to provide another page.


1 Answers

When the the web browser engine reads an HTML document and identifies a script element, it immediately invokes the JavaScript interpretator and executes the code. So, if your document starts with a JavaScript which redirects away from the page, the client shouldn't be shown the remaining document. Something like this could work:

<!DOCTYPE html>
<html>
  <head> 
    <script type='text/javascript'>
        //using "replace" removes the current page from browser history
        location.replace('page_b.html');
    </script>

Also, if there is something on the current page that should not be displayed to the client while the redirect is in process - you can inject some additional CSS, like

<style type='text/css'>
   body {display:none}
</style>
like image 90
naivists Avatar answered Oct 02 '22 08:10

naivists