943,147 Members | Top Members by Rank

Ad:
Mar 10th, 2010
0

Ajax jquery and append

Expand Post »
Hi all, first time post here.

I'm using jquery/ajax to create some links with window.open method. Here's the relevant code:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. $("#content").empty();
  2.  
  3. $.ajax({
  4. type: "GET",
  5. url: "webapps.xml",
  6. dataType: ($.browser.msie) ? "text" : "xml",
  7. success: function(xml){
  8. var newXML = parseXml(xml);
  9.  
  10. $("#content").append('<h2>'+ cat + '</h2>');
  11. $("#content").append('<div id="appLinks"></div>');
  12.  
  13. $(newXML).find("APP").each(function(){
  14. <!-- alert("before if"); -->
  15. if ($(this).parent().attr('name')==cat){
  16. $("#appLinks").append('<a href=\"javascript:window.open(\"' + $(this).find("LINK").attr("VALUE") + '\",\"' + $(this).find("WINDOW").text() + '\",\"' + $(this).find("FEATURES").text() + '\");\">' + $(this).find("TITLE").text() + '</a><br />');
  17. alert('<a href="javascript:window.open("' + $(this).find("LINK").attr("VALUE") + '","' + $(this).find("WINDOW").text() + '","' + $(this).find("FEATURES").text() + '");">' + $(this).find("TITLE").text() + '</a><br />');
  18. }
  19. });
  20. }
  21. });
  22. };

Basically, when you click a link a function is called with a parameter based on the particular link you run. Then the code runs through an xml file, and if the parent of the nodes I've cyling through has a value equal to the parameter past to the function, that node is used to create a new link with window.open function attached to it.

It all works, or seems to, and when I alert what is being built, it looks right to me, yet the links don't work.

I've attached a copy of one of the alerts of one of the links as it's built.

Can anyone help?

Thanks,
cfh
Attached Thumbnails
Click image for larger version

Name:	sshot.gif
Views:	60
Size:	10.3 KB
ID:	13973  
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cfHutton is offline Offline
3 posts
since Mar 2010
Mar 10th, 2010
0
Re: Ajax jquery and append
cf,

I can't immediately see why it's not working however two things occur to me:
  • On repeated use the ajax success function would create more than one div with id="appLinks". I think this would cause $("#appLinks").append(......) to fail on second and subsequent calls (maybe without a Javascript error).
  • If the alert works without escape characters, then the append(...) shouldn't need them either.
Airshow
Sponsor
Reputation Points: 300
Solved Threads: 357
WiFi Lounge Lizard
Airshow is offline Offline
2,520 posts
since Apr 2009
Mar 10th, 2010
0
Re: Ajax jquery and append
But I've just noticed $("#content").empty(); at the top there. So my first point is probably incorrect.

Sorry, even less help than I first thought.

Airshow
Sponsor
Reputation Points: 300
Solved Threads: 357
WiFi Lounge Lizard
Airshow is offline Offline
2,520 posts
since Apr 2009
Mar 18th, 2010
0
Re: Ajax jquery and append
Airshow,

Thanks for the reply. Sorry I didn't get back to you sooner but got swamped and then had a weird issue trying to log in here...

On the alert thing and the escapes, I was just trying so many different things that they got a little out of sync. I don't think the escapes were needed at all, but I wanted to test so I put them in both the actual code and the alert function that mirrored it. Then got another idea and removed the escapes from the code and just didn't worry about the alert.

The odd thing is that the alert (sans the escapes) shows me exactly what I need, which is a link created from xml that uses the window.open function to open a new window with a given set of features and the link text is what I want. However, when the code quits running and I have my list of links on the page, and I hover over one and look in the status bar, all I get is:

"javascript: window.open("

without the parens.

So, it seems that the jquery/ajax is creating the exact string I need, but somehow it doesn't make it into the page (even though the alert is AFTER when it should be loaded).

I can get the job done with xslt, but I just thought this would be a more efficient manner and worth learning more about.

Thanks again for the reply,
cfh
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cfHutton is offline Offline
3 posts
since Mar 2010
Mar 18th, 2010
1
Re: Ajax jquery and append
cf,

I have had another look at the code and the alert gif and I think it's a simple question of avoiding nested double quotes in the composed HTML.

Change :
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. $("#appLinks").append('<a href=\"javascript:window.open(\"' + $(this).find("LINK").attr("VALUE") + '\",\"' + $(this).find("WINDOW").text() + '\",\"' + $(this).find("FEATURES").text() + '\");\">' + $(this).find("TITLE").text() + '</a><br />');
to:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. $("#appLinks").append('<a href="" onclick="window.open(\'' + $(this).find("LINK").attr("VALUE") + '\',\'' + $(this).find("WINDOW").text() + '\',\'' + $(this).find("FEATURES").text() + '\'); return false;">' + $(this).find("TITLE").text() + '</a><br />');
Nested doubles are substituted for singles.

I tested as far as I can with alert and document.write() and my suggested code certainly parses out OK - no javascript error and the win.open() works when the resulting link is clicked.

Airshow
Sponsor
Reputation Points: 300
Solved Threads: 357
WiFi Lounge Lizard
Airshow is offline Offline
2,520 posts
since Apr 2009
Mar 19th, 2010
0
Re: Ajax jquery and append
Click to Expand / Collapse  Quote originally posted by Airshow ...
cf,

I have had another look at the code and the alert gif and I think it's a simple question of avoiding nested double quotes in the composed HTML.

Change :
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. $("#appLinks").append('<a href=\"javascript:window.open(\"' + $(this).find("LINK").attr("VALUE") + '\",\"' + $(this).find("WINDOW").text() + '\",\"' + $(this).find("FEATURES").text() + '\");\">' + $(this).find("TITLE").text() + '</a><br />');
to:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. $("#appLinks").append('<a href="" onclick="window.open(\'' + $(this).find("LINK").attr("VALUE") + '\',\'' + $(this).find("WINDOW").text() + '\',\'' + $(this).find("FEATURES").text() + '\'); return false;">' + $(this).find("TITLE").text() + '</a><br />');
Nested doubles are substituted for singles.

I tested as far as I can with alert and document.write() and my suggested code certainly parses out OK - no javascript error and the win.open() works when the resulting link is clicked.

Airshow
Hey, thanks Airshow!

Haven't had a lot of time to test it, but it seems to work in ie6 which is my primary target. Firefox 3.6 seems to work, too, but there's at least one instance of a discrepancy in the way they treat the 'features' with firefox allowing an address bar on a window where it was suppose to be turned off. That's no big deal though because the apps the links point to won't function in firefox anyway.

Have to examine further the quote thing. I thought I had mixed them properly, using one set (singles) basically to delineate string literals, and the other set (doubles) as quotes that were themselves PART of those string literals....

Anyway, thanks again for your help. It's much appreciated.

cfh
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cfHutton is offline Offline
3 posts
since Mar 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Delete confirm message not working with ajax
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: JS call to PHP to XML file, back to PHP and then back to JS (im confused LOL)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC