0

here is my code:

a.html :

<script>
 $(function(){
  $('#A').load('b.html');
  $('h1').click(function(){
    functionB();
  });
 });
</script>
<html>
 <h1>Title</h1>
 <div id='A'></div>
</html>

and,

b.html :

<script>
  function functionB(){
   alert('funcation B called');
  }
</script>
<p>nothing</p>

seems that I can not call functionB() from root. is there anyway to do? thanks

4
  • 2
    which child element. those are two different pages! Commented Jan 17, 2014 at 10:34
  • 2
    You would either have to load the b.html (with either Ajax or an IFrame) to be able to access functionB();. Alternatively you could place functionB(); in an External JS File and reference it on both pages. Commented Jan 17, 2014 at 10:36
  • What you are trying to do is a bad, bad idea. Nunners' suggestion to put the scripts in a separate (javascript) file is the correct one. If you need to run functionB on both pages, then include the script on both pages. There is NEVER a good reason to mix HTML and JS together and 'call a function on another page'. It just means your basic design is wrong. Commented Jan 17, 2014 at 10:44
  • 1
    @Nunners: Suggest you post your idea as an answer... It is a better way of organising this specific example. Rather than another take credit for your suggestion. Commented Jan 17, 2014 at 10:52

2 Answers 2

1

You should abstract the functionB() function into a separate .js file and include that file on each page:

Any time you have common code, abstract it into a shared component and call that component from the places where it's needed.

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

4 Comments

its my personal suggestion .
In that case: Please give credit to the commenter that suggested this before you :)
Hi, I found this is working from the test: wesley.com.hk/test2 but donno why do not work on the site I actually working on. I am developing an ajax site with hash natvigation. each inner page has it's own js functions. then, i am going to add a 'on hash change' function so to allow user press 'back' of the browser. so I need to call functions on the inner page so to let it open
your code is working in me also; the functionB() is calling in A.html page. are you sure that b.html is as exact as you've mentioned here ?
0

I found what I've done wrong:

right b.html

<script>
  $('#bContent').ready(function(){
    //do something here
  });
  function functionB(){

    alert('funcation B called');
  }
</script>
<p id='bContent'>nothing</p>

wrong b.html

<script>
  $('#bContent').ready(function(){
    //do something here
    function functionB(){
      alert('funcation B called');
    }
  });
</script>
<p id='bContent'>nothing</p>

Thanks for all your help.

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.