CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2005
    Posts
    52

    Question passing parameters problem

    hi
    i am trying to send the parameters to the popup window from js function.
    Now this is my js function and i am trying to send parameters to popup.

    var newwindow = '';
    function popitup(url,st,ed)
    {
    newwindow=window.open(url,'name','height=400,width=350');
    var txt = st+'<br>'+ed
    newwindow.document.body.innerHTML += txt;
    }

    this is the call to the function
    popitup("\main.html","10","20");

    This is javascript in my popup window(html) page
    var begin = ' ';
    var end = ' ';
    document.write(begin)
    document.write("<br>")
    document.write(end)
    document.write("<br>")


    but this is not working......
    whats the problem?how should i send the parameters?i want to write that paaramets in popup window.

  2. #2
    Join Date
    Jun 2005
    Posts
    1,255

    Re: passing parameters problem

    A little remark: quotes have been forgotten in 'height=400','width=350'. But even without the quotes, it is working well for me with the following lines:
    Code:
    <html>
    <head>
    <script language=javascript>
    var newwindow = '';
    function popitup(url,st,ed)
    { 
    newwindow=window.open(url,'name','height=400','width=350');
    var txt = st+'<br>'+ed
    newwindow.document.body.innerHTML += txt; 
    }
    </script>
    </head>
    <body">
    <a href="javascript:popitup('popupwindow1.htm','10','20');">Click here and a window will pop up</a>
    </body>
    </html>
    and popupwindow1.htm contains
    Code:
    <html>
    <head>
    </head>
    <body>
    <h1>Hi, I am a pop up window</h1>
    <script language=javascript>
    var begin = ' ';
    var end = ' ';
    document.write(begin)
    document.write("<br>")
    document.write(end)
    document.write("<br>")
    </script>
    </body>
    </html>
    I suppose that the problem is

  3. #3
    Join Date
    Jun 2005
    Posts
    1,255

    Re: passing parameters problem

    Another solution is:
    Code:
    <html>
    <head>
    <script language=javascript>
    var newwindow = '';
    function popitup(url,st,ed)
    { 
    newwindow=window.open(url,'name','height=400','width=350');
    newwindow.document.getElementById('text1').innerHTML += st;
    newwindow.document.getElementById('text2').innerHTML += ed;
    }
    </script>
    </head>
    <body">
    <a href="javascript:popitup('popupwindow1.htm','10','20');">Click here and a window will pop up</a>
    </body>
    </html>
    and
    popupwindow1.htm contains:
    Code:
    <html>
    <head>
    </head>
    <body>
    <h1>Hi, I am a pop up window</h1>
    <script language=javascript>
    document.write("<span id='text1'>start:</span>")
    document.write("<br>")
    document.write("<span id='text2'>end:</span>")
    document.write("<br>")
    </script>
    </body>
    </html>

  4. #4
    Join Date
    Jul 2005
    Posts
    52

    Question Re: passing parameters problem

    i got the solution for my above problem...now this is new question i want to ask regarding the above problem ......
    i have this function
    var newwindow = '';
    function popitup(url,st,ed)
    {

    newwindow=window.open(url,'name','height=400,width=350');

    var txt = st+'<br>'+ed

    newwindow.document.getElementsByTagName('body')[0].innerHTML=txt;


    }
    AND THE CODE WRITTEN IN THE BODY IS

    alert("Click OK ");
    popitup("\trial.html","10","20");
    alert("click ok alert2");
    popitup("\trial.html","30","40");

    now here 10 20 first gets displayed in popup window and then 30 40 gets diaplayed in popup window overwriting the 10 20
    but i want to display both outputs in one window one below other...
    how can i do this?
    infact whenever i make a call to this function the o/p should get appened to previous o/p in the popup window
    please help me.

  5. #5
    Join Date
    Jul 2005
    Posts
    52

    Question Re: passing parameters problem

    I am using this code.....and checking weather the window is open or not and trying to append the o/p but still i am not getting o/p as expected....


    this is my function

    var newwindow = '';
    var txt ='';
    function popitup(url,st,ed)
    {
    if (!newwindow.closed && newwindow.location)
    {
    newwindow.location.href = url;
    }
    else
    {
    newwindow=window.open(url,'name','height=400,width=400');
    if (!newwindow.opener) newwindow.opener = self;
    }

    if (window.focus) {newwindow.focus()}

    txt= st+'<br>'+ed
    newwindow.document.getElementsByTagName('body')[0].innerHTML=txt;
    }

    still if i make consecutive function call like this ...

    popitup(url,10,20)
    popitup(url,30,40)

    the output is :
    first 10 20 gets displayed and then 30 40 is displayed in the same window overwriting 10 20.....i dont want that...i want 10 20 followed by 30 40.

    how to do that?please help me....
    thank you

  6. #6
    Join Date
    Aug 2005
    Posts
    2

    Re: passing parameters problem

    there are several ways to do this. an easy (and untidy) way is to store what is supposed to be stored in innerHTML in global var.

    e.g.

    Code:
    var txt1="";
    var txt2="";
    
    function popitup(url,st,ed)
    {
    
    	//blah blah blah
    
    	txt1+= " "+st;
    	txt2+= " "+ed;
    	
    	newwindow.document.getElementById('text1').innerHTML = txt1;
    	newwindow.document.getElementById('text2').innerHTML = txt2;
    }
    hope this helps.
    reminder: do note that innerHTML is IE specific. You need to modify things here and there for it to work in other browsers. My advice is to use W3CDOM and use the [object].appendChild method.

    another possible improvement is to store the data as 2 arrays instead of strings so that you can iterate and process them later if necessary. You can write a specialized function to format an array into a string for display purposes. A quick and dirty example is as follows:

    Code:
    var array1 = new Array;
    var array2 = new Array;
    
    function popitup(url,st,ed)
    {
    
    	// blah blah blah
    	
    	array1.push(st);
    	array2.push(ed);
    	
    	newwindow.document.getElementById('text1').innerHTML = format_array_to_string(array1);
    	newwindow.document.getElementById('text2').innerHTML = format_array_to_string(array2);
    }
    
    function format_array_to_string(a)
    {
    	if(!a.length)
    		return "";
    		
    	var sOutput = "";
    	
    	for( i = 0; i < a.length; ++i )
    		sOutput += ( i? " ":"" ) + a[i];
    
    	return sOutput;	
    }
    This also makes it possible to sort/remove items and then still get the correct output display.

    hope this helps.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured