Steven
Steven

Reputation: 345

jquery.load not working with files that contain partial html

I have an empty scriptblock:

 <script id="target" type="text/html"></script>

I am using

 jQuery("#target").load(...)

to load the contents of a few seperate files into a target scriptblock

To simplify, file1 contains starthtml, file2 contains some various html containing ALL begin AND end tags and file3 contains endhtml

file 2 loads perfect, file 1 and 3 however will not load OR just load partially while I get a success status from the load. It seems like there is some contruction (not ending started tags or vice verca) or some character in the htmlcode in my template that will not load into the scriptblock.

Here is the real content that will not display in my codeblock after loading:

        </td>
        <td width="1px" style="background-color:#CCCCCC;"></td>
    </tr>
    <tr>
        <td colspan="3" height="1px" style="background-color:#CCCCCC;"></td>
    </tr>
</table>

<!-- *** END WRAPPER TABLE *** -->

</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</body>
</html>

<!-- *** END HMTL *** -->

If I replace the content above with more simple html like blow, then it works fine

 </td></tr></table>

What could be the cause and is there a solution or workaround?

Upvotes: 2

Views: 489

Answers (1)

Steven
Steven

Reputation: 345

Ok so I found the source of the problem and the solution.

The problem with jQuery.load for what I want to do is that it will set the .html of the target element. setting the .html with jQuery will cause jQuery to parse/evaluate the content that is returned from the server for valid html since the .load method is created as a shortcut for loading content into your page.

If jQuery.ajax is used the parsing will not take place, it just gets the result from the server and I can do any processing myself, including putting the content in the element I want. instead of setting the content of the scriptblock with .html I can now set it using .text which will not parse the content.

This way I can set any content in my scriptblock which will be used later to to create a html template/layout for display in a textarea so it is available to the user to copy.

Upvotes: 3

Related Questions