0

Hi We using a master template for all aspx pages. In master page we added frequent using CSS, JS, In aspx page pages we using require CSS and JS for that particular page.

Now we are trying add Jquery Ajax or XmlHttp for website optimization. Using Jquery ajax we calling a aspx page into DIV. Here problem when we load total aspx page from HTML tags javascript and all scripts working fine. But when we trying to load that page from particular DIV or body then scripts not working.

Can please tell me how we can load scripts when loading page from particular DIV or body.

2
  • show the code you are working with, and how is script not working? Are you getting errors in the console?CommentedMar 15, 2014 at 10:26
  • @PatrickEvans $("#mainContentSwitcher").html($(result).find("#ContentSwitcher").html()); scripts are not being loaded when i use the above line. I need html part of a particular div
    – Bangar
    CommentedMar 15, 2014 at 10:36

1 Answer 1

1

In your aspx page make sure you do not have relative paths for your CSS and JS files. The following is using a relative path. If you load the file through an ajax request and into a div the relative paths will not work.

 <link rel="stylesheet" type="text/css" href="../Common/css/myCss.css" media="all" /> <script type="text/javascript" src="../Common/js/myJs.js"></script> 

Instead you must use the absolute path of the file

 <link rel="stylesheet" type="text/css" href="<%=Page.ResolveUrl("../Common/css/myCss.css") %>" media="all" /> <script type="text/javascript" src="<%=Page.ResolveUrl("../Common/js/myJs.js") %>"></script> 

Try this and see if it works. I have not tested it in the way you want to use it.

Edit:

If you are receiving the page as an html string, instead of doing this:

 $(result).find("#ContentSwitcher").html() 

try the following:

 $.parseHTML(result).find("#ContentSwitcher").html() 

Note: $.parseHTML requires jquery 1.8.0 or higher.

8
  • it's not working. There is no problem with CSS files only issue with Script and JS files.
    – Bangar
    CommentedMar 15, 2014 at 11:27
  • This works fine for me when I call my admin.aspx page from my about.aspx page like so: $.ajax("<%= Request.Url.GetLeftPart(UriPartial.Authority)%>/Secure/Admin.aspx", { success: function (result) { $(".content").html(result); } });CommentedMar 15, 2014 at 11:45
  • yes there is no problem while loading the entire aspx page but i want only a part of it for ex: <html><body><div id="contentSwitcher"><script></script><ul><li></li></ul></div></body></html> In this case i want to load only contentSwitcher div which contains the script code
    – Bangar
    CommentedMar 15, 2014 at 11:58
  • <script type="text/javascript" src="<%=Page.ResolveUrl("/o/one.js")%>"></script>
    – Bangar
    CommentedMar 15, 2014 at 12:25
  • Is the directory "o" directly off the root of the project? If not, it may be that the path to the js is wrong. This is the only other problem I can think of. Everything else looks fine.CommentedMar 15, 2014 at 12:44

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.