Javascript functions do not work in IE - php

I am using a function in conjunction with FusionCharts. I am using a function that gets rid of the charts if no data can be found for it. The function is from FusionCharts.
It runs fine with Firefox and Chrome but screws the whole Drupal page in IE. Can you look at the code and see if I improperly wrote it. Thanks.
<script type='text/javascript'><!--
FusionCharts('Yield_Tab_3a_Growth_of_100000').addEventListener (
['NoDataToDisplay', 'DataXMLInvalid'],
function() {
FusionCharts('Yield_Tab_3a_Growth_of_100000').dispose();
}
);
</script>

Remove the <!--. You should not be using these comment tags anymore. https://stackoverflow.com/a/808850/897559

As others have mentioned, the primary issue is that you're missing the end to your comment, but the more modern way to prevent JavaScript from interfering with your HTML markup is to use a CDATA block:
<script type='text/javascript'>
//<![CDATA[
FusionCharts('Yield_Tab_3a_Growth_of_100000').addEventListener (
['NoDataToDisplay', 'DataXMLInvalid'],
function() {
FusionCharts('Yield_Tab_3a_Growth_of_100000').dispose();
}
);
//]]>
</script>
Though that's not really necessary in this case either because you're not using any < or & symbols in your code.

I see a missing comment tag that you started at the beginning. IE might be a little more sensitive to that
<script type='text/javascript'><!--
FusionCharts('Yield_Tab_3a_Growth_of_100000').addEventListener (
['NoDataToDisplay', 'DataXMLInvalid'],
function() {
FusionCharts('Yield_Tab_3a_Growth_of_100000').dispose();
}
);
//-->
</script>
The line I added is right above

You seem to be missing a //--> before your </script> tag:
<script type='text/javascript'><!--
FusionCharts('Yield_Tab_3a_Growth_of_100000').addEventListener (
['NoDataToDisplay', 'DataXMLInvalid'],
function() {
FusionCharts('Yield_Tab_3a_Growth_of_100000').dispose();
}
);
//-->
</script>
Without that, you have a unclosed HTML comment. That could mess up your page big time.
--> : End of the HTML comment
// : Comment out the --> in your JavaScript, to prevent syntax errors.
However, nowadays you can just remove the comment altogether:
<script type='text/javascript'>
FusionCharts('Yield_Tab_3a_Growth_of_100000').addEventListener (
['NoDataToDisplay', 'DataXMLInvalid'],
function() {
FusionCharts('Yield_Tab_3a_Growth_of_100000').dispose();
}
);
</script>

Related

How can I make this div element dissapear

I have this code on my website and this div appears when u successfully post a comment.
Now I want the div element disappear after 3-5 seconds. Here's my code:
echo "<div class='granted'><p>Comment posted.</p></div>";
echo "<script type=\"text/javascript\">
setTimeout(function() {
$('.granted').fadeOut('fast');
}, 3000);
</script>";
Thanks!
Your code works perfectly. I think you have missed to link the script library.
Adding this line before the PHP script does the trick.
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
Your code should work perfectly. You can also use the delay() function:
$('.granted').delay(3000).fadeOut('fast');
$(document).ready(function(){
setTimeout(function(){
$('.granted').hide();}, 5000);
});
It's not good practice to include HTML tags inside php codes.
<div class='granted'><p>Comment posted.</p></div>
<script>
$(function(){
setInterval(function(){
$('.granted').fadeOut(1);
},5000);
});
</script>

jQuery post() to php page

I'm learning php/javascript so don't smile...
I try from page1.php to post 3 variables to page2.php.
I'm not sure what's wrong...
Here is the code (simplified mode):
page1.php
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
window.onload = post_text;
function post_text() {
test1="111";
test2="222";
test3="333";
$.post("page2.php", { test1:test1 , test2:test2, test3=test3 });
}
</script>
</body>
</html>
page2.php
<?php
$a=$_POST['test1'];
$b=$_POST['test2'];
$c=$_POST['test3'];
echo $a.$b.$c;
?>
$.post("page2.php", { test1:test1 , test2:test2, test3:test3 });
Since you are learning, you might try to isolate problems by writing shorter chunks of code and seeing if they work first. In this case your first problem is an ordinary typo (test3=test3, instead of test3: test3) so your whole JS does not parse. You should be seeing the relevant error message in the firebug console (or chrome console).

Jquery : .replace ® not functioning

Why doesn't this work?
<script type="text/javascript" src="js/jquery-1.8.1.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("body").html($("body").html().replace(/®/g, '<sup>®</sup>').replace(/®/g, '<sup>®</sup>').replace("\u00AE" , '<sup>®</sup>'));
};
);
</script>
Firebug in Firefox gives this in the console:
"SyntaxError: missing ) after argument list"
But, this breaks the Wordpress entirely...
<script type="text/javascript">
$(document).ready(function() {
$("body").html($("body").html().replace(/®/g, '<sup>®</sup>').replace(/®/g, '<sup>®</sup>').replace("\u00AE" , '<sup>®</sup>'));
}
);
</script>
The basic of this function is to find every single registration mark in the body, and replace it with "®"
If this function is capable in PHP as well, that's preferable.
It seemed like the best thing to do was to go into the database and just run a sql query to replace all registration marks and wrap it in "" tags.
However, this did work on individual elements. (Like adeneo said, it's not a good idea to replace the entire site HTML... which broke the site for me)
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"> </script>
<script type="text/javascript">
$(document).ready(function() {
$("h1").html(
$("h1").html()
.replace("®", "<sup>®</sup>")
.replace(/®/g, '<sup>®</sup>')
.replace(/®/g, '<sup>®</sup>')
.replace("\u00AE" , '<sup>®</sup>')
);
});
</script>

Change attribute of HTML elements of all tag?

Is there a way to change a particular attribute of all HTML elements of a certain tag with javascript or php (or anything else)? For example, I'd like to use javascript to change all "<a>" elements on the page to have the "target='_blank'" attribute. Any suggestions?
getElementsByTagName will grab all your anchors and then you can iterate over them:
var list = document.getElementsByTagName("a"), len = list.length;
while( len-- ) {
list[len].target = "_blank";
}
JSFiddle: http://jsfiddle.net/zwbCS/
The following jQuery code will add a target="_blank" attribute to all anchor (a) elements on a page:
$(function() {
$('a').attr('target', '_blank');
});
You will need to include a copy of jQuery in your HTML's head element, before the script above. So your head element will look something like:
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('a').attr('target', '_blank');
});
</script>
</head>
First of all, you do not seem to distinguish the difference between server-side languages (like PHP), and client-side (like Javascript). Read up on the difference here.
Answering your question, though. The best option (and the easiest IMHO) for you is to get jQuery and use the following script:
$(document).ready(function() {
$('a').attr('target', '_blank');
});
Take a look at using jquery you should be able to do something like
$("a").attr("target", "_blank");
Here is a JQuery example, in case you want to want to open in new window only the external links:
var myHosts = [ // just in case you are using full abs paths
'www.mydomain.com'
];
jQuery(function()
{
jQuery( 'a[href^="http"]' ).each( function()
{
if ( jQuery.inArray( this.hostname, myHosts ) == -1 && this.href.indexOf( location.hostname ) == -1 )
jQuery( this ).attr('target','_blank');
});
});
IMHO forcing visitors to do it your way is not a good idea. Modern browsers have easy to use shortcut keys for opening of the links in new window/tab. So if the visitor found your site so incredibly useful, he will back to you, even if he leave your site by clicking on external link.
If you want to make a hint to your visitors about external links on your site, I suggest you to put an icon in the link(by CSS for example) and to modify and use the script from above to append desired CSS class to that links. Like:
jQuery(function()
{
jQuery( 'a[href^="http"]' ).each( function()
{
if ( jQuery.inArray( this.hostname, myHosts ) == -1 && this.href.indexOf( location.hostname ) == -1 )
jQuery( this ).addClass('external_link ');
});
});
CSS:
.external_link { padding-right: 20px; background: url(path/to/the/image) no-repeat right top; }

PHP and jQuery function—only works once?

I have the following function. When I click the first time, it returns a random number, but all subsequent clicks always return the same number. How come it doesn't refresh?
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#btn-get-random-image').click(function () {
$('#my-img').attr('src', '<?php echo $pics[array_rand($pics, 1)]; ?>');
});
});
</script>
It's because you're using PHP to generate the random number, and it can't possibly be refreshed across calls to the JS function -- it's embedded in the HTML by that point.
May be you can also use live like instead of click
$(document).ready(function(){
$('#btn-get-random-image').live("click", function () {
// your works here
}
});
also check out jquery live
As others have said, you are using PHP, which is executed once on the server and sent as raw output, so the image will not change. Try this!
Edit: modified code to make it suck less.
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
var myPics = <?php echo json_encode($pics); ?>;
$(document).ready(function() {
$('#btn-get-random-image').click(function () {
var index;
do {
index = Math.floor(Math.random() * myPics.length);
} while ( typeof myPics[index] == 'undefined' );
$('#my-img')
.attr('src', myPics[index]);
});
});
</script>
This uses PHP's JSON encoder to dump your array to the javascript, then your function randomly selects an image from that array to display. The do/while loop might not be necessary, my testing shows a pretty good near-uniform distribution over thousands of iterations, but there it is nonetheless.
Your PHP is creating JS code, which gets sent to the browser once when the page is rendered. Each call to the JS function runs the function as it existed when the page was rendered.

Categories