Hi all, first time post here.

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

$("#content").empty();

  $.ajax({
    type: "GET",
    url: "webapps.xml",
    dataType: ($.browser.msie) ? "text" : "xml",
    success: function(xml){
	   var newXML = parseXml(xml);
	   
	   $("#content").append('<h2>'+ cat + '</h2>');
	   $("#content").append('<div id="appLinks"></div>');

	   $(newXML).find("APP").each(function(){
		<!-- alert("before if"); -->
			if ($(this).parent().attr('name')==cat){
				$("#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 />');
				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 />');
			}
		});
	}
	});
};

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

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

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

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

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 :

$("#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:

$("#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

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 :

$("#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:

$("#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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.