Why is my onclick event not registered in Firefox? - javascript

I have a list item with an onclick event. It runs in Chrome and Internet Explorer, but not Firefox. Any suggestions?
<li onclick="alert('test');">test test<br></li>

This works fine for me in Firefox.
Check this:
JavaScript is enabled in your browser.
Try adding a return false; statement in your tag.
Or a function like this:
function test() {
//your code here
return false;
}
Or use this:
Link
or this
Link

I was trying to minimize my html code to send a complete code to simulate the error as Boris Zbarsky requested. Then I found my mistake.
I was using marquee tag which has been deprecated. Now I am going to use jQuery instead of it.
thx

In Firefox, the event object is not global. You have to access it within your script tags not in html.
onclick works likes this
<li id="alert">test<br></li>
<script>
document.getElementById("alert").addEventListener("click", function( event ) {
alert('test');
}, false);
</script>

Attributes can be ignored by Firefox when it is served invalid HTML, use
https://validator.w3.org/
to clean up the HTML.

Related

Javascript and target="_top"

I'm working with a legacy frames website that was just moved into an iFrame.
Assuming I have the following function:
<script language = "javascript">
function myFunction(){
<!-- no console.log in IE 7 (my required target browser) -->
alert('sup, yo?');
}
</script>
and the following hyperlink triggering the function:
click me
before the move into an iFrame this worked ok. Once the website was moved into the iframe, clicking the link in IE (not FF or Chrome), I would get the ever-so-helpful error:
Line: 1
Object expected
Once I removed the target="_top" attribute the function would work, so I don't need help solving the problem, but my question is:
What is IE doing with the target attribute when calling a javascript function to invoke this behavior? I don't have other versions of IE installed, is this current behavior in 8+ as well?
Thanks.
It does not make sense to try to understand the behavior. You're using a technique that is not well defined and is not used by developers nowadays.
Instead of href="javascript:myFunction();, just use onclick="myFunction(); return false" or even better, set the handler from JS like the following
<a href="pageForUsersWithoutJs.html" id="my-link" >click me</a>
<script type="text/javascript">
// This is old school, but works for all browsers, you should use a library instead
document.getElementById('my-link').onclick = function() {
// Do your thing
return false; // so the link isn't followed
};
</script>

jQuery onclick placer doing nothing

I have an input on my index page:
<input type="button" name="goReplaceAll" value="Go" id="allReplaceGo" style="cursor:hand;" />
And an onclick call from jquery:
$(document).ready(function() {
$('#allReplaceGo').click(replaceAll);
});
function replaceAll(){ alert('a'); }
When I click the button, nothing happens. When I instead use onclick="replaceAll()" within the HTML, I get an error: Uncaught ReferenceError: replaceAll is not defined
I am unable to figure out a connection between these errors, or any possible cause.
The code is currently live at http://www.texturepacker.net
Edit: Looks like I get two different results in Firefox and Chrome, Chrome does nothing while Firefox alert('a')'s once the page loads. Now I'm just plain confused?
Edit: Seemingly an unrelated syntax error later in my code was breaking the call. Now replaceAll() is called when the dom loads, my question is now why isn't replaceAll() being launched onclick and instead once the dom loads, am I missing something obvious?
Look like a syntax error here...
Line 196 in you script file says
$(this).("src",tmpSrc);
It is supposed to be
$(this).attr("src",tmpSrc);
Fix that and it should be all fine..
Also on line 7
$('#allReplaceGo').live("click",replaceAll());
is supposed to be
$('#allReplaceGo').live("click",replaceAll);
Also as of version 1.7 .live() is deprecated. try using .on() instead
That happens, probably, because the function replaceAll is outside the jQuery scope.
Try to put her inside the $(document).ready scope.
Your code should look like
$(document).ready(function() {
$('#allReplaceGo').click(function(){
replaceAll()
});
});
function replaceAll(){ alert('a'); }

javascript print iframe doesn't work in firefox

I have tested this in Chrome and all good, but Firefox is lazy.
Part of HTML in question:
<div id="print" style="display:none;"></div>
And Javascript follows it like:
$('.print').click(function(){
printF($(this));
return false;
});
function printF(dugme){
$('#print').html("<iframe src='http://example.comli.com/index.php/prijava/pogledaj/"+$(dugme).attr('name') +"' onLoad='this.contentWindow.print();'></iframe>");
};
Thank you
Is the problem that the click event is not getting fired or that your IFRAME is not getting loaded?
Anyway, I think I got this working in Firefox. I had to put your click function into the document#ready event though. Like this:
$(function(){
$('.print').click(function(){
printF($(this));
return false;
});
});

How to check dynamic javascript value with Jquery with IE

I am stuck on this, please help!
I have an external Javascript that inserts code on my page. Among other things it inserts an image wrapped in a div. I do not have control over the script, but I would like to change the image path/url using Jquery.
This is what I have done:
$('.ProductImage img').attr('src',function(index,attr){
return attr.replace('small','original');
});
Works like a charm in all browsers except IE.
When checking the selector with alert(), IE returns %Thumbnail% which is the Javascript variable/object. I have tried wrapping my script in a timeout to allow IE to finish loading but no luck.
Any ideas?
Thanks in advance!
Have you tried wrapping your code inside $(function(){ .. }) so that it will run after the document finished loading?
If your script is not loaded by the time your code gets executed you could try putting the code inside window.onload
window.onload = function(){
replaceImages();
};
function replaceImages(){
$('.ProductImage img').attr('src',function(index,attr){
return attr.replace('small','original');
});
}

Click event not worked in Firefox

I am using a jquery click function:
Button code
<input type="button" id="myButtton">
Jquery code
$(document).ready(function () {
$("#myButtton").click(function () {
alert("Mybutton");
});
});
This code works in Internet Explorer but does not work in Firefox.
What am I doing wrong?
In the code:
$(document).ready(function(){
$("#myButtton").click(function(){
alert("Mybutton");
});
I believe it's missing another closing brace:
$(document).ready(function(){
$("#myButtton").click(function(){
alert("Mybutton");
});
});
My best guess is that you have other input with the same ID? Try using classes instead, or use jQuery's CSS selector like $('input[type=button]') instead.
I'd also recommend installing FireBug plugin for FireFox if you haven't done so already (http://www.getfirebug.com/). It'll help you debug JavaScript issues like this, and a whole lot more.
Are you sure that element has an id attribute? Or does it have only a name attribute with a value of "myButton". In order to work cross browser the id attribute is mandatory, whereas name is optional (only IE and Opera AFAIK).
N.B.: My answer may seem idiot, but it was not the original poster that added the code example in the question (view edit history).

Categories