0

My page needs to grab a specific div from another page to display within a div on the current page. It's a perfect job for $.load, except that the HTML source I'm pulling from is not necessarily well-formed, and seems to have occasional tag errors, and IE just plain won't use it in that case. So I've changed it to a $.get to grab the HTML source of the page as a string. Passing it to $ to parse it as a DOM has the same problem in IE as $.load, so I can't do that. I need a way to parse the HTML string to find the contents of my div#information, but not the rest of the page after its </div>. (PS My div#information contains various other div's and elements.)

EDIT: Also if anyone has a fix for jQuery's $.load not being able to parse response HTML in IE, I'd love to hear that too.

4
  • Most browsers are okay with ill-formed HTML. Can you post a sample that IE actually fails on? Commented Nov 29, 2010 at 20:50
  • It's a pretty large page, and I'm not sure where exactly the error is, but all signs are pointing to that being the cause. Then again I could be completely wrong. Commented Nov 29, 2010 at 20:55
  • Post the malformed HTML, this is a something jQuery should be able to do very well. The rendering engine would not be involved in the parsing by $.load... Commented Nov 29, 2010 at 20:57
  • It's most likely a duplicate element ID issue. See my updated answer. Commented Nov 29, 2010 at 20:59

2 Answers 2

1

If the resource you are trying to load is under your control, your implementation spec is poorly optimized. You don't want to ask your server for an entire page of content when you only really need a small piece of that content.

What you'll want to do is isolate the content you want, and have the server return only what you need.

As a side note, since you are aware that you have malformed HTML, you should probably bite the bullet and validate your markup. That will save you some trouble (like this) in the future.

Finally, if you truly cannot optimize this process, my guess is that you are creating an inconsistency because some element in the parsed HTML has the same ID as an element on your current page. Identical ID's are invalid and lead to many cross-browser JavaScript problems.

Sign up to request clarification or add additional context in comments.

7 Comments

I agree with this, but unfortunately not an option.
Could you explain? There's always a way. ;)
It's not my data, so I'm pulling it out of an HTML page that already exists, since there is no API to get it from.
I was so concerned with making sure the #information id was unique I never stopped to consider that other IDs might be duplicated. Checking that out now...
Yeah there are quite a few duplicate IDs between the two: we use the same template (again, outside of my control). This works in everything else but I guess IE just can't parse it with any duplicated IDs.
|
0

Strictly with strings you could use a regular expression to find the id="information" tag contents. Never parse it as html.

I'd try the $.load parameter that accepts a html selector as well

$('#results').load('/MySite/Url #Information');

1 Comment

This was working for me in everything except IE, which does get the HTML response, but then can't find the div#information, even when I rewrote it to do it more explicitly with a $.get and $.find

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.