最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How redirect to other page using HTML or JavaScript - Stack Overflow

matteradmin7PV0评论

Im begginer in WebProgramming so maybe my questuion will seem naive to some of you. I want to run a simple JavaScript function without any click to implement a redirection.

I try this:

@inherits System.Web.WebPages.WebPage
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery")
</head>
<body>
     <a href="/OauthCallBack/GmailOAuthCallback"></a>
</body>
</html>

But it dosen't work.

Any idea wat I am missing?

Thank you in advance.

Im begginer in WebProgramming so maybe my questuion will seem naive to some of you. I want to run a simple JavaScript function without any click to implement a redirection.

I try this:

@inherits System.Web.WebPages.WebPage
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery")
</head>
<body>
     <a href="/OauthCallBack/GmailOAuthCallback"></a>
</body>
</html>

But it dosen't work.

Any idea wat I am missing?

Thank you in advance.

Share Improve this question asked Aug 11, 2014 at 8:04 MichaelMichael 13.6k59 gold badges170 silver badges315 bronze badges 4
  • You haven't got any JavaScript in the code you've shared. (But if you want to do a redirect, then do it with an HTTP Location header, not with JavaScript). – Quentin Commented Aug 11, 2014 at 8:07
  • Your title states "HTML or Javascript" but your question only only mentions javascript. If you're happy with HTML, then look at META redirection <meta http-equiv="refresh" content="0;URL='http://www.example./'" /> – freefaller Commented Aug 11, 2014 at 8:08
  • Your probobly missing the text for your <a> link (^_^) – Stuart Commented Aug 11, 2014 at 8:11
  • You must write some text between anchor open tag<a> and close tag </a>, – Aditya Commented Aug 11, 2014 at 8:38
Add a ment  | 

4 Answers 4

Reset to default 5

Simply use window.location="http://www.newlocation.". See the example below:

<head>
<script type="text/javascript">
<!--
function Redirect()
{
    window.location="http://www.newlocation.";
}

document.write("You will be redirected to main page in 10 sec.");
setTimeout('Redirect()', 10000);
//-->
</script>
</head>

More info here

You can use Meta tag for client side redirection.

<META http-equiv="refresh" content="5;URL=http://www.example.">

Which will redirect the user to http://www.example. after 5 seconds.

window.location is a property of window object.

You can try function version too

function myCustomHref() { window.open ('http://www.newlocation.', '_self'); }

Here you can find more optional properties

if you want to redirect from a webpage to another using javascript,then you can try the following script,

<script>
function newPage() {
    window.location.assign("http://www.w3schools.")
}
</script>

For full details you can view the following website,

http://www.w3schools./js/js_window_location.asp

Post a comment

comment list (0)

  1. No comments so far