0

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! :)

Harmen
22.5k4 gold badges57 silver badges77 bronze badges
asked Nov 28, 2010 at 15:07
2
  • 2
    Please 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. Commented Nov 28, 2010 at 15:09
  • You should learn the basics of Javascript. Commented Nov 28, 2010 at 22:40

2 Answers 2

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();)

answered Nov 28, 2010 at 22:08
Sign up to request clarification or add additional context in comments.

14 Comments

Ok, so the correct code would then be $ (Shadowbox.clearCache(); Shadowbox.setup();).appendTo('#photo-gallery'); ?
@Prodac: No, it wouldn't. Do you know what the two lines are?
@SLaks: No I don't, thats why I posted the question in the first place. I'm pretty novice when it comes to js...
As I said in the answer, you should replace your bad line. jsfiddle.net/SLaks/aQyJM
@Prodac: appendTo is used to add HTML elements to other HTML elements. You're calling Javascript methods, which are not connected to HTML elements.
|
2

You can't have a </script> inside a script. Change

$('<script type="text/javascript"></script>')

to

$('<script type="text/javascript"><\/script>')
answered Nov 28, 2010 at 15:14

3 Comments

@SLaks: When you say "get rid of it", do you then mean remove '<script type="text/javascript"></script>' from $('<script type="text/javascript"></script>').html('Shadowbox.clearCache(); Shadowbox.setup();').appendTo('#photo-gallery');???
@Prodac, I think @SLaks means use $('<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/>');
What SLaks meant was for me to replace $('<script type="text/javascript"></script>').html('Shadowbox.clearCache(); Shadowbox.setup();').appendTo('#photo-gallery'); with hadowbox.clearCache(); Shadowbox.setup();. That works perfectly :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.