Javascript window.location.href onClick not working - javascript

So I have a basic html page and basic Javascript in the head. I'm trying to redirect using Javascript once the function is activated through onClick but window.location.href wont work. window.alert works and everything else.
HTML
<html>
<head>
<title>Testing Aff Links</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
function myfunc(){
window.location.href="http://youtube.com";
}
</script>
</head>
<body>
Link Link #1
</body>
</html>

You need to return false from the onclick attribute to prevent the default action:
Link Link #1

You are redirecting twice. Change it to:
Link Link #1

Related

Open whatsapp from website using javascript [duplicate]

Essentially, all I want to do is open an external web page after the current page is loaded via java script.
open my page -> javascript tells browser to open external page -> external page being loaded into the broser
How may I accomplish this?
you may use this
<html>
<head>
<script type="text/javascript">
function load()
{
window.location.href = "http://externalpage.com";
}
</script>
</head>
<body onload="load()">
<h1>Hello World!</h1>
</body>
</html>
<html>
<head>
<script type="text/javascript">
function load()
{
window.location.href = "http://externalpage.com";
}
</script>
</head>
<body onload="load()">
<h1>Hello World!</h1>
</body>
</html>
Hope it should be window.location. Check the code.
Technically you can:
location.href = "http://example.net/";
… but you should perform an HTTP redirect instead as that is more reliable, faster and better food for search engines.
You can also use the "open" method to open the source file or url on a new window.
window.open("anyfile.*");
or
window.open("http://anylocation.com");
Hi please try this code for page loading time we will redirect whatever u configured the urls.
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
window.open('https://google.com');
}
</script>
</head>
<body onload="myFunction()">
</body>
</html>
<body>
<script>
document.body.innerHTML += 'Link';
document.getElementById("link").click();
</script>
<body>

How do I link external Javascript file to a html document in Play Framework project?

This is my scala.html file code.
#()
<!DOCTYPE html>
<html>
<head>
<title> Spin To Win </title>
<link rel="stylesheet" media="screen" href="#routes.Assets.versioned("stylesheets/styles.css")">
<script type="text/javascript" src='#routes.Assets.versioned("scripts/test.js")'></script>
</head>
<body>
<h1> This is title </h1>
<input type="button" value="click" onclick="showAlert()">
</body>
</html>
This is my javascript file.
function showAlert() {
alert("Hello");
}
This is the file directory where the javascript is stored.
Output
CSS works correctly.
But Javsascript doesn't work. Nothing happens when I click the button.
Update
So if I just put a alert in the test.js outside of any function, it works. The alert is displayed when the page loads.
alert("hello");
So, this means the js file is linked properly.
But if I put the code in a function like earlier and link it to a button it doesn't work.
You aren't actually executing your function.
Your code should be:
function showAlert() {
alert("Hello");
}
showAlert();
For buttons, it should be:
<button onclick="showAlert()">Button</button>

Prevent default event (browser navigation) when user clicks anchor tag

I have a table in html like below. What I want is to prevent user to direct to google.com when they click John via Javascript or Jquery. I tried it like below but it did not work. Where is my mistake?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script>
$(function() {
$(".myTDclass").click(function(event) {
event.preventDefault();
alert('prevented');
});
});
</script>
</head>
<body>
<table>
<tr class="myTRClass">
<td><a href='www.google.com' class='myTDclass'>John</a></td>
<td>Smith</td>
</tr>
</table>
</body>
</html>
onclick='false' is a solution, I know. But i need it in js or jquery. Any help would be appreciated.
There are several issues here. Firstly your jQuery code is, for some unknown reason, in a style tag when it should be in a script tag. Secondly, you haven't included jQuery in the page at all. Lastly the code itself needs to be in a document.ready event handler if you place it in the head of the document.
Once you fix those issues it should work fine. Try this:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script>
$(function() {
$(".myTDclass").click(function(event) {
event.preventDefault();
alert('prevented');
});
});
</script>
</head>

How to disable right click on a hyperlink in page/popup open in Iframe using javascript/jquery?

I need to disable right click on a Anchor tag/hyperlink in page/popup open in iframe using javascript/jquery?
I have below code logic which works when I open page in iframe but not work when I open popup in Iframe by clicking on link within the Iframe page.
In below example when I right-click on Open Popup link its not allowing the right click but when I open popup by clicking Open Popup link it's not working for links in popup.
Any suggestions?
Below sample code snippet for reference -
TestPage.html
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<title>Test Page</title>
</head>
<body>
<iframe id="sampleIframe" src="PopupPage.html" width="400" height="150"></iframe>
<script language="javascript" type="text/javascript">
$('#sampleIframe').load(function() {
$('#sampleIframe').contents().find('a').each(function() {
$(this).on("contextmenu", function() {
return false;
});
});
});
</script>
</body>
</html>
PopupPage.html
<html>
<head>
<title>Popup Page</title>
</head>
<body>
You are on popup page.
Open Popup
<script language="javascript" type="text/javascript">
function openPopup() {
window.open("LinkPage.html", null,
"height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
}
</script>
</body>
</html>
LinkPage.html
<html>
<head>
<title>Link Page</title>
</head>
<body>
Goto Google
Goto Facebook
</body>
</html>
For this to work, you need to include your script to disable the links in the actual iFrame content.
To demonstrate, create a jsfiddle with this code (it will not let me save it to post a link)
This would be your "LinkPage.html"
HTML
Open Popup
JAVASCRIPT
function openPopup() {
window.open("http://jsfiddle.net/t45qvtap/1/show", null,
"height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
}
And you will see it working.
Just to explain further, this is the contents of the fiddle that the above code will open in an iFrame, this would be your "PopupPage.html"
HTML
This content is in an iframe
<div>
Goto Google
Goto Facebook
</div>
JAVASCRIPT
$('body').contents().find('a').each(function() {
$(this).on("contextmenu", function() {
return false;
});
});

Javascript autoclick not working

I am using a script to make tables in html sortable. The script is here- http://www.kryogenix.org/code/browser/sorttable/. I want the text which sorts the html table to be clicked automatically when the i loaded. The autoclick script i am using is this-
<head>
<script LANGUAGE='javascript'>
function autoClick(){
document.getElementById('sort').click();
}
</script>
</head>
<body onload="autoClick();">
<table><tr><th><p id="sort">Click here to sort the table</p></th>...
The problem is that this is not working and i am confused that why this isnt working.
--------------------EDIT------------------
Sorry for this but actually i was typing something wrong in the body onload statement. Thus the script i was using was correct.
Where have you defined your event?
Because I see juste one function in your onload.
Below, a little example which work fine:
<html>
<head>
<script type='text/javascript'>
var init = function()
{
document.getElementById('test').addEventListener('click', function() {
alert('Auto test is ok');
}, false);
};
function autoClick(){
document.getElementById('test').click();
}
</script>
</head>
<body onload="init(); autoClick();">
<button id="test">Test</button>
</body>
</html>
It's always safer to use Jquery library. Just include the latest Jquery library on your header section of the page:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js" type="text/javascript"></script>
and this function Should solve your issue:
$("#sort").live('click');
I think you missed the class name in the table
Please add your table tag with class name called "sortable".

Categories