I’m having issues using .get calls to update elements via page load or button press.
The code…
$(document).ready(function() {
//updateVariable();
$('#dannychoolink').html('<?php dannyChoo()?>');
$('.danny-choo').attr('target', '_blank');
});
$('#clicky').mouseenter(function() {
$('#dannychoolink').html('Click for a Random DannyChoo.com Article');
}).click(function() {
updateVariable();
$('#dannychoolink').html('YA!');
//$('#dannychoolink').html('<?php dannyChoo()?>');
//$('.danny-choo').attr('target', '_blank');
});
function updateVariable() {
$.get('random.php',
function(output){
$('#dannychoolink').html(output);
$('.danny-choo').attr('target', '_blank');
}
);
};
The function updateVariable() calls random.php which holds a single function which echos a database entry like this:
<a href="http://www.dannychoo.com/post/en/26568/Nendoroid+Moekana.html" class="danny-choo">Nendoroid Moekana</a>
The idea is to load a random entry (which is done inside the function) either on page load and click.
This works perfectly on my home page (http://www.danielbough.com), but does not work as expected on individual articles (the updateVariable function actually returns a WHOLE page of html identical to an individual article page – if that makes sense.) Any page w/one article does it (http://www.danielbough.com/articles/fun-with-jquery-and-ajax for example.)
Each page is built w/individual files. The ‘homepage’ is
- header.file
- navigation.file (the dannychoolink and clicky element
reside here) - homepage.file
- footer.file
Individual articles are as so:
- header.file
- navigation.file (the dannychoolink and clicky element
reside here) - view.file
- footer.file
I thought I might have some unmatched tags in view.file but there wasn’t any that I could see.
I had to comment out the updateVariable() call in my document.ready function because it kept loading until the page crashed.
JavaConsole shows 'Uncaught SyntaxError: Unexpected token < ' when the individual article pages are loaded or when I click the ‘clicky’ element. I cant determine why.
Note, Google+ and Twitter calls are in the view.file too. I removed them while testing, but it has no affect.
UPDATE
The 'Uncaught SyntaxError: Unexpected token < ' errors were caused by ckeditor.js and Googles jquery.min.js. I’ve updated the Google js library to the ‘full’ version of 1.7.2 and removed the ckeditor.js to test. The java errors went away, but the issue with the get function remains.
You are trying to access a file via a relative path, in this case
random.php, from two different locations on your server. In the article’s case you are trying to get http://www.danielbough.com/articles/random.php which just gives you a regular page with no content which is why you were pulling in that entire html.The solution would be to give random.php an absolute url, which I believe is http://www.danielbough.com/random.php. So something like:
$.get('http://www.danielbough.com/random.php',