SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry Help me with javascript

    Help me with javascript

    I have one file have name: 1.html writed code:
    <HEAD>
    <TITLE>WINDOWS</TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    function openWindow(url,name) {
    popupWin = window.open(url, name, "scrollbars=yes,width=800, heigth=200 ");
    }
    function m(a) {
    popupWin = alert('Click ok');
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <a name="123" href="javascript:m(this.a)">oPen</a>,<br>
    <input type="button" value="-link-" onclick="m(this.a)">

    </BODY>
    </HTML>
    -------------------
    i want to write one file with name 2.html
    can control fuction m(a) of file 1.html
    when open 2.html will run m(a) and show "click ok"
    Help me! Thanks everyone

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,824
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Move the JavaScript into a separate file so you can link it into both pages.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to use a this page to control a another page.you help yourselves!

  4. #4
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    As I understand it you want a button on 1.html to fire an alert on 2.html. The following script does that, but you need to move the new window away from the main window so that the alert doesn't hide behind the window holding 1.html.

    The function with the alert is part of the the new page 2.html.

    // ===== 1.html ================

    <html>

    <head>
    <title>This is 1.html</title>
    <script type="text/JavaScript">
    <!--
    // global ref used to communicate with new window
    var popupWin=null;
    //
    // open new window
    function openWindow(urlA,nameA)
    { // check to see if popupWin already exists
    if(!popupWin || popupWin.closed)
    { popupWin=window.open(urlA, nameA, "scrollbars=yes,width=200,height=200");
    }
    else
    { popupWin.focus();
    }
    }
    // ======== end open window =========
    //-->
    </script>
    <style type="text/css">
    <!--
    #main { position:absolute; top:100px; left:100px; width:100px; height:100px; text-align:left; }
    #main p { margin-top:0px; margin-bottom:10px; }
    -->
    </style>
    </head>

    <body>

    <div id="main">
    <p class="zz"><a name="123" href="javascript: openWindow('2.html','winA')">Open</a></p>
    <p><input type="button" value="-link-" onclick="window.popupWin.m()">
    </div>
    <!-- end main -->

    </body>

    </html>

    // ======= this is 2.html ================

    <html>

    <head>

    <title>This is 2.html</title>
    <script type="text/javascript">
    <!--
    function m()
    { alert('Click ok');
    }
    //-->
    </script>
    </head>

    <body>

    <p>This is your new window. Click the link in the first window to fire the alert
    in this window</p>

    </body>

    </html>

  5. #5
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    file_1 (parent window) contains the function you want to run.

    When you open file_2 (child window), the function in the parent window will be automatically called.

    file_1.htm

    Code:
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    <script type="text/javascript">
     
    function showAlert() {
     alert("Function from file_1 was called");
    }
     
    </script>
    </head>
    <body>
     
    <h1> This is file 1</h1>
     
    <p>
     <a href="#" onclick="window.open('file_2.htm'); return false;">Open file 2</a>
    </p>
     
    </body>
    </html>
    file_2.htm

    Code:
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    <script type="text/javascript">
     
    window.onload=function() {
        var openerWin = window.opener;
        openerWin.focus();
        openerWin.showAlert();
    }
     
    </script>
    </head>
    <body>
     
    <h1> This is file 2</h1>
    
    </body>
    </html>

  6. #6
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    file_1 (parent window) contains the function you want to run.

    When you open file_2 (child window), the function in the parent window will be automatically called.

    file_1.htm

    Code:
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    <script type="text/javascript">
     
    function showAlert() {
     alert("Function from file_1 was called");
    }
     
    </script>
    </head>
    <body>
     
    <h1> This is file 1</h1>
     
    <p>
     <a href="#" onclick="window.open('file_2.htm'); return false;">Open file 2</a>
    </p>
     
    </body>
    </html>
    file_2.htm

    Code:
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    <script type="text/javascript">
     
    window.onload=function() {
        var openerWin = window.opener;
        openerWin.focus();
        openerWin.showAlert();
    }
     
    </script>
    </head>
    <body>
     
    <h1> This is file 2</h1>
    
    </body>
    </html>
    ------>
    ok!Thanks very much u! u undestanded think of me! But i want call a function from web another.So i can't call
    Example: function showAlert() input at http://example.com/file_1.html
    And i can't change this.
    Can u help me call function showAlert from another website? Thanks!

  7. #7
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vlbknl2 View Post
    ------>
    ok!Thanks very much u! u undestanded think of me! But i want call a function from web another.So i can't call
    Example: function showAlert() input at http://example.com/file_1.html
    And i can't change this.
    Can u help me call function showAlert from another website? Thanks!
    Why do you want to call a function in a html file on another website. Is the other website someone else's website or is it yours?

  8. #8
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    Why do you want to call a function in a html file on another website. Is the other website someone else's website or is it yours?
    I can use to investigate the function from the file. html not. because if it is. js, you can use the src = "http://example/file.js." but if a file is ". html" from a different page, you can call it?

  9. #9
    SitePoint Member
    Join Date
    Oct 2010
    Location
    China
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    first,i think this is not a right design philosophy.
    u can try to use the following way.
    1.html
    <HEAD>
    <TITLE>WINDOWS</TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    function openWindow(url,name) {
    popupWin = window.open(url, name, "scrollbars=yes,width=800, heigth=200 ");
    }
    function m(a) {
    popupWin = alert('Click ok');
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <a name="123" href="javascript:m(this.a)">oPen</a>,<br>
    <input type="button" value="-link-" onclick="m(this.a)">
    </BODY>
    </HTML>
    2.html
    <html>
    <head>
    </head>
    <body>
    <iframe style="visibility:hidden" src="1.html" id="ifm"></iframe>
    <input type="button" value="button" onclick="ifm.m();" />
    </body>
    </html>

  10. #10
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, thanks u! But when click button on 2.html , the function of 1.html don't Run

  11. #11
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Choky View Post
    first,i think this is not a right design philosophy.
    u can try to use the following way.
    1.html


    2.html
    Oh, thanks u! But when click button on 2.html , the function of 1.html don't Run

  12. #12
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, thanks! your file can run on IE but don't run on FIREFOX

  13. #13
    SitePoint Member
    Join Date
    Oct 2010
    Location
    China
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i don't know that u use firefox,sorry,it's my mistake.
    u can try as following.It can be run on IE and Firefox after my test.
    2.html
    <html>
    <head>
    </head>
    <body>
    <iframe style="visibility:hidden" src="1.html" id="ifm"></iframe>
    <input type="button" value="button" onclick="document.getElementById('ifm').contentWindow.m();" />
    </body>
    </html>

  14. #14
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Choky View Post
    i don't know that u use firefox,sorry,it's my mistake.
    u can try as following.It can be run on IE and Firefox after my test.
    2.html
    Ok, thanks u very much! But when i upload to host then don't run
    I upload file to host: http://muangay123.com/2.html and http://muangay123.com/1.html

    But the function don't run
    Help me ! Thanks everybody!

  15. #15
    SitePoint Member
    Join Date
    Oct 2010
    Location
    China
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i visited your site:http://muangay123.com/2.html ,then i found that there was something wrong with the value of src,check your code
    <iframe style="visibility:hidden" src="http://www.muangay123.com/1.html" id="ifm"></iframe>
    "http://www.muangay123.com/1.html" and "http://muangay123.com/2.html" is not in the same domain,so you do not have the authority to call the function

  16. #16
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Choky View Post
    i visited your site:http://muangay123.com/2.html ,then i found that there was something wrong with the value of src,check your code

    "http://www.muangay123.com/1.html" and "http://muangay123.com/2.html" is not in the same domain,so you do not have the authority to call the function
    I try check again. Resulf is : something the function called but something can't call this function !


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •