Link to home
Start Free TrialLog in
Avatar of egarciat
egarciat

asked on

Javascript Jquery UI concern

I used to build all my web based apps using frames, later I switched to iframes, and now it seems that iframes are not very well accepted (XHTML Strict), so I switched to object tag.

Internet explorer, does not behave well with <object> tag, It does weird things that other browsers don't. (I hate it).

Today I "discovered" Jquery, and it's UI plugin excellent tool. However there is something that concerns me.

let's say that I have the following code.

<html>
head....
styles....
scripts...
<body>
<div id="x">
</div>
</body>

In some point of the code I add some action for a click event, that loads div "x" with code from another html document using jquery's load function.

$('#x').load('myotherpage.html');

It works like a charm, however.. what happens to the <html> head styles scripts etc.. from the other page?

Are they lost?, combined?.. should I avoid adding lots of stuff on these areas on the page to be loaded on to a div?..  In general what are the best practices when using this load() techinque..?

I could create some server side script that sends only "body".. but it would require to recode many many old code..

So my main concern is: is it correct to send a complete HTML document including body head scripts styles etc.. as a response to the load() function?

Avatar of egarciat
egarciat

ASKER

I forgot to menction, that the to-be-loaded-in-a-div content, is generally complex and mostly contains forms, the forms are standard however an script (in the head area) controls most of the operations of the form such as client side validation, click handlers for dynamic data etc..  So it is not possible to create a single script on the initial page that handles all the events for all possible forms that could be loaded in a div. They had to be undependant of each other.

I would venture a guess that it is grabbing whatever is inside the <body></body> and placing that as the content of the element you are loading it into. So anything above <body> or below </body> would be removed. Since I can't find documentation on their site that says this is so, I am just making an educated guess.

If you would put the javascript inside the <body> tag it would probably include it.

hmm, actually i just tested this, and

it removes these tags

<html>
<head>
</head>
<body>
</body>
</html>

but includes everything inside those tags, except it completely removes any <script> blocks

so it doesn't look like you can include javascripts at all with it.

You could try building an ajax request to do the same, except it wont auto strip out those parts. Then i think you would need to eval the result in order to execute any javascript.
Avatar of lenamtl
What Ii usually do is to embed the javascript page in the Head,
duplicate my javascript and give different name variable
and then call a div class name
<div class="section1">

I don't know if this is a best practice but it is working for me.

I used this method often, specially when I have several different UI calendar in a form.
ASKER CERTIFIED SOLUTION
Avatar of ikbentomas
ikbentomas
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I just did some testing, and found that jquery load is smart enough. It behaves almost, ALMOS like an iframe or frame or object.

All I had to do was to include the container in all calls of javascript inside the "other page". $('#myapp #mywhatever').doSometing instead of $('#mywhatever')...
and it works, no unexpected behavior nor errors in the console, so I will continue in that direction.

Thanks for all suggestions.