0

I am loading .html content with jquery .load() but the javascript and css is not applied to the one.html/two.html after it is loaded by the .load() function.

Example below:

<head>
    <style> styles1 </style>
    <link href="stylesheets/style2.css" rel="stylesheet">
    <script src="script1.js">
    <script>     
        $(document).ready(function(){
           $("button.os").click(function(){
              fillmein = $(this).attr("target");
              fillmeinfile = $(this).attr("target")+'.html';
              $("."+fillmein).load("/contentfolder/"+fillmeinfile);
           });
        });

     ...
     other scripts
     ...
    </script>
</head>

<body>
   <p>the css and any js is applied to this block</p>
   <button class="os" target="one">replace</button>
   <div class="one">I will be replaced by one.html</div>
   <button class="os" target="two">replace</button>
   <div class="two">I will be replaced by two.html</div>
</body>

I understand that the one.html/two.html is loaded after the styles and javascript is loaded by the browser but how I get the styles and javascript that is in the <head> to apply to the newly loaded one.html/two.html?

I new to jQuery so let me know how I clarify if needed. thanks!

EDITED Thanks for providing answers everyone! Updated the code example to clarify what I meant.

copying the <style> and <script> into the one.html and two.html works but if I load the javascript twice it could conflict. for example, having logic that searches $(document), and functions that collapse and expand a section can be called multiple times. Is it possible to have the js and css that was loaded in the main page work on the newly loaded .html files or is there any clean and DRY way to do this?

5
  • what is the content of content.html Commented Oct 21, 2016 at 3:26
  • 4
    see plnkr.co/edit/7bYF2ynE92X0as6lsqCh?p=preview Commented Oct 21, 2016 at 3:30
  • can you also post your js and css code ? Commented Oct 21, 2016 at 3:38
  • thanks arun, that works but ran into issues when multiple scrips were copied over from the loaded .html files which caused problems/conflicting with the scripts in the original main html file. i updated my question. thanks! Commented Oct 21, 2016 at 4:45
  • 1
    As far as keeping it clean and dry -- you could have the Javascript apply only to specific objects instead of using the $(document) selector You could also set some kind of COOKIE or server-side session var to indicate that you dont want to run a specific script because it has already been run... Local Storage is an even better way... I think you can check if a function already exists as well in a conditional statement Commented Oct 22, 2016 at 1:00

2 Answers 2

1

As suggested by Arun P Johny in his comment

You simply put your CSS inline on the target document and it will be automatically loaded along with the content.

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

Comments

0

You can write

$('button').on('click','Classname',function(){
  //write your structure here
  })

This will work for all tags with 'Classname',which are already present or dyanamically added later on.

Comments

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.