SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to modify javascript function to take more arguments.

    Hi, I'm new to Java
    script with very little knowledge... I have an external javascript file with certain attributes defined to open a file in new window, which I use for viewing maps. However I need to define different attributes for the height and width, as I want users to open video clips in smaller windows. What do I need to do? Many thanks.

    Here is the code:

    // JavaScript Document
    function initpop()
    {
    var closeElementId='closewindow';
    var closeLinkText='close window';
    var windowAttributes='width=640,height=620,left=0,top=0,scrollbars=no,location=no';

    if(!window.opener)
    {
    var as,i,popfun
    as=document.getElementsByTagName('a');
    for (i=0;i<as.length;i++)
    {
    if(as[i].className=="zoom")
    {
    popfun=function(){window.open(this.href,'',windowAttributes);return false;};
    as[i].onclick=popfun;
    as[i].onkeypress=popfun;
    }
    }
    } else {
    var closep,closelink,closetext;
    closelink=document.createElement('a');
    closetext=document.createTextNode(closeLinkText);
    closelink.href='#';
    closelink.appendChild(closetext);
    closelink.onclick=function(){self.close();};
    if(document.getElementById('closewindow'))
    {
    document.getElementById('closewindow').appendChild(closelink);
    }
    }
    }

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function initpop(width,height) {
    	var closeElementId='closewindow';
    	var closeLinkText='close window';
    	var windowAttributes='width=' + width + ',height=' + height + ',left=0,top=0,scrollbars=no,location=no';
    
    the rest is the same...
    Then just call it like: initpop(640,480)

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Many thanks for this.
    Just a quick question: At the moment I call it like this:
    <a href="video01.htm" class="zoom" target="_blank">watch video</a>

    How would I incorporate: : initpop(640,480) in this?

  4. #4
    SitePoint Member
    Join Date
    Jan 2007
    Location
    Cologne, Germany
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The function you have at the moment just takes all links in the whole document with a classname of "zoom" and adds the onclick and onkeypress properties to the that triggers the opening of the popup. I suppose initpop() is called by window.onload = initpop or <body onload="initpop()">.
    That means that you cannot put the width and height arguments in the function call. You nevertheless could use another class name to trigger different parameters
    Code:
            for (i=0;i<as.length;i++) {
                if(as[i].className=="zoom") {
                    popfun=function() {
                        window.open(this.href,'','width=640,height=620,left=0,top=0,scrollbars=no,location=no');
                        return false;
                        
                    }
                } else if (as[i].className=="map") {
                        popfun=function() {
                        window.open(this.href,'','width=640,height=620,left=0,top=0,scrollbars=no,location=no');//other values here
                        return false;    
                    }
                }
                as[i].onclick=popfun;
                as[i].onkeypress=popfun; //I don't think this is even required, as onclick is also triggered by using the appropriate key on the keyboard
                }
            }

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,608
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    window.onload = function() {initpop(640,480)};
    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="^$">

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you could embed the width/heitgh into the classname, so:

    <a href="video01.htm" class="zoom240x180" target="_blank">watch video</a>
    <a href="video02.htm" class="zoom640x480" target="_blank">watch video</a>

    then your setup function can look for classes matching that pattern and strip out the width and height.


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
  •