simple javascript not working - javascript

I've been having trouble with javascript. I don't think its my code but possible some outside factor. I've tried the following code on chrome and firefox and I can't get the alert to pop up. Nothing happens when I click the link. The code below is obviously not on my site, but I'm just using it as an example as to why other parts of my javascript aren't working.
<html>
<head>
<script language="javascript">
function art() {
alert("jdsklfs");
}
</script>
</head>
<body>
<a href='#'>click</a>
</body></html>

Well, try calling it ;~)
function art() {
alert("jdsklfs");
}
window.onload = art; //<= now the function will execute on page load
or provide the href with an id (<a href='#' id='artclick'>click</a>) and assign a click handler to it on load
function art() {
alert("jdsklfs");
}
window.onload = function(){
document.getElementById('artclick').onclick = art;
}

You are not calling the art function.
Simplest, but dirtiest is to have:
click

try adding
onclick="art();"
to your anchor tag

Change the
<a href='#'>click</a>
to
click

art() isn't tied to the link in any discernible way.

Change your a tag to:
click

Related

Auto Click on hyperlink

I would like to a HTML code with the following functions if you guys can help , please.
Once the page loads, after 1 second to auto click on a hyperlinked text , how do I do this ? For the new website to be opened in the same window.
I have this code but this one is sending me to a new window and the pop up is blocking - not good
<!DOCTYPE html>
<html>
<body>
<head>
<script>
function autoClick(){
document.getElementById('linkToClick').click();
}
</script>
</head>
<body onload="setTimeout('autoClick();',700);">
<a id="linkToClick" href="http://www.google.com" target="_blank">GOOGLE</a>
</body>
</body>
</html>
Thanks
Instead of using <a href=> you should try just setting the variable window.location.href. So your code should look something like this:
<body onload="window.location.href='http://www.google.com',700);">
https://www.w3schools.com/howto/howto_js_redirect_webpage.asp
Let me preface this by saying this is extremely questionable when it comes to web design, and generally regarded as a shady or bad practice. That said:
$(window).on('load', function(){
window.location = 'http://example.com'
});
OR (vanilla JS)
window.onload = function(){ window.location = 'http://example.com' };
Again, be careful with this. If you want to do something like this, you need a pretty good reason why.
Possible duplicates:
Redirect from an HTML page
HTML redirect on page load

Android sending sms onload JS

Hello i use this script on my landing pages, but the user needs to click a button for it to work.
<a onclick="ClickTrack('click5')" href="smsto:NR?body=TEXT" class="btn_2" >
<strong><span class="cta-clik">Click to send sms</span></strong>
</a>
Does anybody know how to make it work without button click but when website Loads?
Personally I dislike the inline bindings but you could try putting it on your body tag
<body onload="ClickTrack('click5')">
If possible bind in your logic or just call it like #randy was saying.
For the redirect part, you can make a function like this:
function redirect()
{
window.location.href = "smsto:NR?body=TEXT"
}
If you use JQuery, you could use document.ready inside your script file:
$('document').ready(funtion(){
redirect();
});
If you don't, include the script in the bottom of your page like this:
<body>
<!-- content here -->
<script>
document.addEventListener("DOMContentLoaded", function(event) {
redirect();
});
</script>
</body>

Make popup on load working

I'm trying to make a pop up right away when page is loading , but all the browsers are blocking it and don't let the pop up works , this is my code :
<html>
<head>
<script>
function codeAddress() {
javascript:void window.open('http://google.com','1411977082597','width=750,height=550,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0');return false;
}
window.onload = codeAddress;
</script>
</head>
<body>
</body>
</html>
There is a solution for that ?
do not need following code
javascript:void
this code use when write javascript on html tags
function codeAddress() {
window.open('http://google.com','1411977082597','width=750,height=550,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0');
}
Check your browser javascript enable.
Check your browser allow popup.
Allow popup to work using Javascript is impossible.
You can do it by calling your method in jquery document.ready method. like:
$(document).ready(function(){
codeAddress();
})

Why is this jQuery click function not working?

Code:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
$("#clicker").click(function () {
alert("Hello!");
$(".hide_div").hide();
});
</script>
The above code doesn't work. When I click on #clicker, it doesn't alert and and it doesn't hide. I checked the console and I get no errors. I also checked to see if JQuery was loading and indeed it is. So not sure what the issue is. I also did a document ready function with an alert and that worked so not sure what I am doing wrong. Please help. Thanks!
You are supposed to add the javascript code in a $(document).ready(function() {}); block.
i.e.
$(document).ready(function() {
$("#clicker").click(function () {
alert("Hello!");
$(".hide_div").hide();
});
});
As jQuery documentation states: "A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute"
I found the best solution for this problem by using ON with $(document).
$(document).on('click', '#yourid', function() { alert("hello"); });
for id start with see below:
$(document).on('click', 'div[id^="start"]', function() {
alert ('hello'); });
finally after 1 week I not need to add onclick triger.
I hope this will help many people
Your code may work without document.ready() just be sure that your script is after the #clicker. Checkout this demo: http://jsbin.com/aPAsaZo/1/
The idea in the ready concept. If you sure that your script is the latest thing in your page or it is after the affected element, it will work.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<a href="#" id="clicker" value="Click Me!" >Click Me</a>
<script type="text/javascript">
$("#clicker").click(function () {
alert("Hello!");
$(".hide_div").hide();
});
</script>
</body>
</html>
Notice:
In the jsbin demo replace http with https there in the code, or use this variant Demo
Try adding $(document).ready(function(){ to the beginning of your script, and then });. Also, does the div have the id in it properly, i.e., as an id, not a class, etc.?
You have to wrap your Javascript-Code with $(document).ready(function(){});Look this JSfiddle.
JS Code:
$(document).ready(function() {
$("#clicker").click(function () {
alert("Hello!");
$(".hide_div").hide();
});
});
Be sure there is nothing on your button (such a div or a trasparent img) that keeps from clicking the button.
It sounds stupid, but sometimes we think that jQuery is not working and all that stuffs and the problem is on the positioning of DOM elements.
You can use $(function(){ // code }); which is executed when the document is ready to execute the code inside that block.
$(function(){
$('#clicker').click(function(){
alert('hey');
$('.hide_div').hide();
});
});
Just a quick check, if you are using client-side templating engine such as handlebars, your js will load after document.ready, hence there will be no element to bind the event to, therefore either use onclick handler or use it on the body and check for current target
Proper Browser Reload
Just a quick check as well if you keep your js files separately: make sure to reload your resources properly. Browsers will usually cache files, so just assure that i.e. a former typo is corrected in your loaded resources.
See this answer for permanent cache disabling in Chrome/Chromium. Otherwise you can generally force a full reload with Ctrl+F5 or Shift+F5 as mentioned in this answer.

link onclick pass object

What i'm trying to do, is pass the object of the link to my javascript function for an ajax query so I can add a loading image next to the link while it's loading.
I thought of wrapping a div around it with the event.
Is this compliant, and will work with all browsers? It seems to work in the one's I have tested, but I only have the newer versions.
<html>
<script>
blah = function(obj){
alert(obj);
return false;
}
</script>
<body>
<div onclick="return blah(this);">work!</div>
</body>
</html>
What I need to be sure of, will doing the return false; on the parent div of the link make sure that when the link is clicked it will not go to the url?
It doesn't seem possible to pass the object of an actual href=. Maybe i'm wrong?
You are wrong. Currently you are not passing the element to your function, but your div.
You can try this:
<html>
<script>
blah = function(objId){
var linkElement = document.getElementById(objId);
var fakeHref = linkElement.getAttribute('fakehref');
alert(fakeHref);
}
</script>
<body>
<a id="link" href="javascript:blah('link');" fakehref="http://www.google.com/">work!</a>
</body>
</html>
Click here to see a working example: http://jsfiddle.net/wYu5M/

Categories

Resources