Reputation: 2777
I have 5 javascript functions index.php :
1) showMsg(); //this display 50 latest messages
2) showPopUpBox(); // this I use jquery to load the textbox and send button from typingMsg.php
3) hidePopUpBox(); // this hide pop up box
4) checkNewMsg(); // this will be auto reloaded every 5 sec to check if there is new message, it will show the numbers counts of total new messages.
5) ShowNewMsg(); // this function will be called when the user click the "show new messages" button.
After a user typing message on the text box on the pop up box then after he click "Send" button, ajax will call messagePost.php to submit the message to database, as the following code :
$(function() {
$(".button").click(function() {
$.ajax({
type: "POST",
url: "messagePost.php",
data: dataString,
success: function() {
$('textarea.expand25-75').val('');
showMsg(); //this is my problem
hidePopUpBox(); //this is my problem too
}
});
return false;
});
});
As u can see from the codes above, the function of showMsg(); hidePopUpBox(); cannot be called because the functions are not on this page, my question is How to call the javascript function from different page?
Upvotes: 3
Views: 14250
Reputation: 13530
You'll have to include all the functions you need in each of the pages that will use them (index.php and messagePost.php, I guess).
That is normally done by grouping all the correlated functions in .js
files and then including them by using the <script>
tag.
By the way, some of the other answers include, correctly, top
and opener
. All of the suggested options make your code work, but in general I suggest you to import the functions right in the pages that need them.
For example, it may make sense to import the hidePopUpBox()
function right within the popup (which will be using than), rather than in the parent window.
Upvotes: 2
Reputation: 100361
Try to use
top.showMsg();
top.hidePopUpBox();
top
refers to the top most window
object of your page. If you are in an iframe for example.
Or try opener
if you are in a popup (called by window.open
)
opener.showMsg();
opener.hidePopUpBox();
Upvotes: 2
Reputation: 2402
no no i can see where is the problem i think the problem is with
$(function() {
// You Are Here Not in contact with outside script
});
i think this is called javascript closure or some thing i think you should use instead of that the
window.onload=function()
{
$(".button").click(function() {
$.ajax({
type: "POST",
url: "messagePost.php",
data: dataString,
success: function() {
$('textarea.expand25-75').val('');
opener.showMsg(); //this is my problem
opener.hidePopUpBox(); //this is my problem too
}
});
return false;
});
}
to be in contact with other functions in same page that if i got you right
Regards
Upvotes: 0
Reputation: 7620
If, as I understand, the above script is run in a popup you can reach the scripts int the base page using opener object.
$(function() {
$(".button").click(function() {
$.ajax({
type: "POST",
url: "messagePost.php",
data: dataString,
success: function() {
$('textarea.expand25-75').val('');
opener.showMsg(); //this is my problem
opener.hidePopUpBox(); //this is my problem too
}
});
return false;
});
});
opener will always exist in a window that was opened from another window pointing back.
Upvotes: 1
Reputation: 76880
What do you mean with "on another page"?If they all are in index.php you should have no problem calling them. Since i see you are using jQuery, i suggest you put your javascript function outside of $(Document).ready(function(){ });
because they have to stay outside, but i don't think it's what you are saying.
Upvotes: 0