javascript in xml

Hi,

I populate an asp.net menu control from the following xml file. Is it possible to use javascript in the xml file. I want to open page1.html (is in <url> tag) in a small window,not toolbar etc. using javascript. If not,is there any alternative way to do it?

<?xml version="1.0" encoding="utf-8" ?>
<menu>
     <menuItem>
     <text>XML Item 1</text>
     <subMenu>
     <menuItem>
     <text>Sub Item 1</text>
     <url>page1.html</url> <--- window.open('page1.htm')... should come here.
</menuItem>

ayha
LVL 7
ayha1999Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sagacitysolutionsCommented:
Try This

<url><a href="page1.html">page1.html</a></url>
or
<url><a href="#" onclick="window.open('page1.htm');">page1.html</a></url>

dorwardCommented:
You can't include scripting in an abritary XML file, since there is nothing to tell the user agent that a take or attribute means "this is scripting".

You should probably be using XSLT to transform the document into a language the browser actually understands and which includes such meaning (XHTML for example) .
dakydCommented:
Don't have an xml parser handy to test this with, but I think CDATA should do the trick - it'll tell the parser to ignore its contents.  So something like this:

<?xml version="1.0" encoding="utf-8" ?>
<menu>
     <menuItem>
     <text>XML Item 1</text>
     <subMenu>
     <menuItem>
     <text>Sub Item 1</text>
     <url><![CDATA[window.open('page.html');]]></url>
     </menuItem>
</menu>

There's a bit more detail at the W3Schools site: http://www.w3schools.com/xml/xml_cdata.asp  Hope that helps.
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

dorwardCommented:
There isn't any data in there which would be interpreted by the parser anyway, so CDATA is pointless.

Rereading the question I now see that I misunderstood it.

You COULD set the URL to be:

javascript:window.open(etc etc etc).

BUT that is very bad practise. http://jibbering.com/faq/#FAQ4_24

To follow best practise you would have to modify the transformation code to include something to deal with popups. (Well, true best practise involves sticking to the window the user provided you with in the first place instead of foisting new popups on them, but http://www.allmyfaqs.com/faq.pl?Console_window is as close as you can come while using popups)
ayha1999Author Commented:
HI everybody,

I tried all of your solution but nothing worked. I think it can be done with CDATA, XSLT etc.  Could anyone try to help to do it?

I will double the points for the help.

thanks in advance.

ayha
dorwardCommented:
It can't be done with CDATA. XSLT might be the solution - but some transformation is already going on, and we would need to see the code that does the transformation in order to tell you how to modify it.
ayha1999Author Commented:
Hi dorward,

I have no XSLT file so far. I have just the xml file containing the menu items. I read this file with xmlreader in asp.net page. the menu control I am using is skmMenu.

ayha
dorwardCommented:
I said "some transformation", not necessarily XSLT.

In this case I suspect the easiest method would be to use the <target> element defined for the XML format the menu uses, and exploit that with some JavaScript.

http://www.skmmenu.com/menu/Download/Docs.aspx

Give the menu items you want to open in new windows a target (such as annoyingPopup), give the container you put the menu in an id (such as mainMenu) and then try this (untested) JavaScript.

function popup(url) {
  if (window.open) {
     window.open(url,'annoyingPopup','width=400,height=300');
     return false;
  }
}

window.onload = function() {
el = document.getElementById('mainMenu').getElementsByTagName('a');
for (i=0; i<el.length; i++) {
  if (el.item(i).target && el.item(i).target == "annoyingPopup") {
                var evn = "return popup('this.href')";
                var fun = new Function('e',evn);
                el.item(i).onclick = fun;
  }
}
}

.... the other options are to stick with the window the user provides you with (always a good choice) or edit skmMenu itself.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ayha1999Author Commented:
Hi dorward,

Where can I stick this function in my page. I am not good in javascript.

ayha
dorwardCommented:
Anywhere. (Personally I'd put in an external file and then add <script src="filename.js" type="text/javascript"></script> to the end of the <head> section of the page, but it will work anywhere - nothing actually happens until the onload event fires.).

Although if you already have an onload event handler, one will probably override the other, so you'll need to integrate the two functions.
ayha1999Author Commented:
Hi,

Now I can open a new window (blank) but at the same time open the page.htm put in the <url> tag.  How can I prevent opening page.htm instead open it in a new new window.

could u pls. modify the code?

ayha
dorwardCommented:
You want the link to work as normal but at the same time open a DIFFERENT page in a popup?

To do that in anything like a sane way you are going to have to modify skmMenu ... and my C# is next to non-existant.
dorwardCommented:
Oh, hang on. I've just reread that and I misread it before. AFAIK that code should have that behaviour already. Do you have a URL so I can see the results in action?
ayha1999Author Commented:
Hi dorward,

I found in the skmmenu site that to open a new windows <target>_blank</target> can be used. How can set the width and height of this windows?

ayha
dorwardCommented:
With the code I provided earlier - except you don't use _blank, you use a named popup.
ayha1999Author Commented:
Hi dorwrd,

Where I can put my page in the function? As I mentioned earlier when I tried the fucntion, I get a new blank window and at the same time redirecting to the original page I want to dispaly in popuop.  I want to prevent redirecting to the page and open that page in new window.

thanks in advance.

ayha
dorwardCommented:
As I said before, putting it anywhere in the page should work.

I have noticed an error in my script though. Change:

              var evn = "return popup('this.href')";

to remove the single quote characters sound "this.href".

If it still doesn't work then, as I said before, I would need to see the generated code. At least a copy/paste of the menu section.
ayha1999Author Commented:
Thanks for the help.

ayha
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.