Solved

Javascript Jquery UI concern

Posted on 2010-11-11
8
706 Views
Last Modified: 2013-11-19
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?

0
Comment
Question by:egarciat
8 Comments
 
LVL 3

Author Comment

by:egarciat
ID: 34114676
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.

0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 34115143
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.

0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 34115219
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.

0
 
LVL 6

Expert Comment

by:stilliard
ID: 34115523
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.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 24

Expert Comment

by:lenamtl
ID: 34115583
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.
0
 
LVL 1

Accepted Solution

by:
ikbentomas earned 125 total points
ID: 34119954
My answer on your question about retrieving complete HTML documents:
I would advise to retrieve only the HTML you need. This can be done two ways:
1. Only save the HTML you need in the file and subtract html, head, body tags etc.
2. Use jQuery load like this $('#result').load('test.html #container');
jQuery will then only load the element with the id container into the element with id result.

I suspected javascript would work and I just did a test to check if javascript is loaded when using jQuery.load. And the inline <script> tags are not inserted in the code but the javascript is executed and stored in the memory. So any javascript function that has been loaded via jQuery.load is not traceable but it is surely present and working.

jQuery load states:
jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document. During this process, browsers often filter elements from the document such as <html>, <title>, or <head> elements. As a result, the elements retrieved by .load() may not be exactly the same as if the document were retrieved directly by the browser.

Another hint if you like jQuery: use the api documentation, it is one of the best documented frameworks available.
0
 
LVL 6

Assisted Solution

by:mickey159
mickey159 earned 125 total points
ID: 34143544
To include Javascript,make it a js file and use $.getScript();

Good luck!
0
 
LVL 3

Author Comment

by:egarciat
ID: 34147160
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.

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now