I have an html page with a random url that I want to be clicked programmatically in javascript

<html>
<head>
<script>

function ClickURL()
{
    document.getElementById('xyz').click();
}
  </script>
</head>

<body onLoad="ClickURL()">

<a href="www.somewebsite.com" id="xyz">Link </a>

</body>
</html>

This only works in IE - it does not work in FireFox. How do I solve this problem?

click() function will not work, but onclick() function will work, for that you have to set onclick='function_name' for <a> element.

see example:

<html>
<head>
<script>

function ClickURL()
{
document.getElementById('xyz').onclick();
}
</script>
</head>

<body onLoad="ClickURL()">

<a href="javascript:alert('hi')" id="xyz" onclick='javascript:alert("click")'>Link </a>

</body>
</html>

here difference is also given.

hi, im new here. i ve a project to automate a process. for which i ve to navigate to a asp.net web page automatically and then to click a link on that navigated page. im in need of asp.net code to do this. i ve tried like this

<html>
<head>
<script language=javascript>
function nav()
{
window.navigate('http://www.w3schools.com/');
}
function lnkchk()
{
For i As Integer = 0 To Me.WebBrowser1.Document.Links.Count - 1
If Me.WebBrowser1.Document.Links(i).InnerHtml.StartsWith("Learn HTML") Then
Me.WebBrowser1.Document.Links(i).InvokeMember("Click")
End If
Next
}
</script>
</head>
<BODY>
<input type="button" onclick="nav(); lnkchk();" value="Click me">
</body>
</html>

but it does not works, i dont know where im wrong since im new to both javascript and asp.net, can anybody pls help me regarding this

at least you both put your homework requests in the same threadIf the page is being generated, to be redirected immediately, why not just put in a redirect in the page <head>, and populate the redirect instead of, or as well as, a link, from whatever (not shown) script is populating the link <META HTTP-EQUIV="Refresh" CONTENT="5; URL=forward_target.html"> the effect is the same, it is browser independent, and standards compliantthere is very little real world application
the redirect would logically be done serverside and the user never see the intermediate page
the course you are taking is BS

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.