I'm trying to implement the following code in a html document:
$(function () {
$.ajax({
type: "GET",
url: "/projects/img/Bathurst/PhotoGallery.xml", // location of your gallery's xml file
dataType: "xml",
success: function(xml) {
$(xml).find('img').each(function() {
var location = '/projects/img/Bathurst/'; // relative path to the directory that holds your images
var url = $(this).attr('src');
var alt = $(this).attr('alt');
$('<li></li>').html('<a href="'+location+''+url+'" rel="shadowbox[gallery]"><img class="thumb" src="'+location+''+url+'" alt="'+alt+'" title="'+alt+'" /></a>').appendTo('#gallery-ul');
});
$('<script type="text/javascript"></script>').html('Shadowbox.clearCache(); Shadowbox.setup();').appendTo('#photo-gallery');
}
});
});
The code works perfectly when I use it in an external .js file, but I cant get it working when i implement it, it just renders with error in the code.
II'm I missing something and dos anyone have a suggestion to this? The reason why I need to implement it, in case some one wonderes, is that I'm building a custom webapp and the line "/projects/img/Bathurst/PhotoGallery.xml" and "/projects/img/Bathurst/" is dynamic variables.
All answers are very much appreciated! :)
-
2Please define "it just renders" and "error in the code." How, exactly, is it failing? Also, how are you embedding it in the document? Please provide just enough encapsulating HTML to reproduce the problem.David– David2010年11月28日 15:09:54 +00:00Commented Nov 28, 2010 at 15:09
-
You should learn the basics of Javascript.SLaks– SLaks2010年11月28日 22:40:07 +00:00Commented Nov 28, 2010 at 22:40
2 Answers 2
The problematic line ($('<script type="text/javascript">...) is a convluted and unnecessarily complicated way to run two lines of Javascript.
You should replace it with simple method calls. (Shadowbox.clearCache(); Shadowbox.setup();)
14 Comments
appendTo is used to add HTML elements to other HTML elements. You're calling Javascript methods, which are not connected to HTML elements.You can't have a </script> inside a script.
Change
$('<script type="text/javascript"></script>')
to
$('<script type="text/javascript"><\/script>')
3 Comments
$('<script type="text/javascript" />') without the close tag. api.jquery.com/jQuery says "Alternatively, jQuery allows XML-like tag syntax (with or without a space before the slash): $('<a/>');