SitePoint Sponsor |
|
User Tag List
Results 1 to 10 of 10
Thread: Modifying a JavaScript Function
Hybrid View
-
Nov 15, 2000, 09:38 #1
- Join Date
- Aug 1999
- Location
- Pittsburgh, PA, USA
- Posts
- 3,910
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi,
I'm using some code to generate a pop-up upon the clicking of a link. Here's the code I have in my HEAD tags:
Code:<script language="JavaScript"> <!-- function PopWindow() { window.open('page.html','popup','width=550,height=400,menubar=no,scrollbars=yes,toolbar=no, location=no,directories=no,resizable=yes,top=50,left=50'); } //--> </script>
<a href="JavaScript:PopWindow()">Open Window</a>
I'd like to, if possible, switch things around a bit so the page is not specified in the function, but so I can specify it each time I call the function, something like this:
<a href="JavaScript:PopWindow(page.html)">Open Window</a>
I've tried modifying the code in several different ways, but an error always follows. Any help would be greatly appreciated.
-
Nov 15, 2000, 09:59 #2
-
Nov 15, 2000, 10:01 #3
Tooo much time :(
Make sure the window.open line is in a single line and not breaks up in 2 lines in your script.
Code:<script language="JavaScript"> <!-- function PopWindow(page) { window.open(page,'popup','width=550,height=400,menubar=no,scrollbars=yes,toolbar=no,location=no,directories=no,resizable=yes,top=50,left=50'); } //--> </script>
<a href="JavaScript:PopWindow('page.htm')">Open Window</a>
Dont forget the Single Quotes in the above code. 'page.htm'
Hope it helps.
[Edit Note] i took much time in replying, it seems. Wayne has already answered.
[Edited by reachmk on 11-15-2000 at 10:05 AM]
-
Nov 15, 2000, 11:04 #4
- Join Date
- Aug 1999
- Location
- Pittsburgh, PA, USA
- Posts
- 3,910
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks guys - surprisingly I came very close to that in my attempt at modification, but I think I had single quotes where I shouldn't have, and lacked them in other places.
Thanks to both!
-
Nov 16, 2000, 20:07 #5
- Join Date
- Feb 2000
- Location
- where the World once stood
- Posts
- 700
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi Chris,
I thought I'd expand on what Wayne and reachmk posted; hope you don't mind.
The passing of arguments (in this case 'page.html') to a function is the better programming design for the following two (at the minimum) reasons:
1) It takes one step away from hard-coding and one step towards OOP (object oriented programming).
2) It enables you to use the same function for any number of applications
If you have a number of pages you need to open with various links, you can put your sources in an array and then access them that way. For example:
var myLinks = new Array();
myLinks[0] = 'page1.html';
myLinks[1] = 'page2.html';
window.open(myLinks[0],'popup',.......blahblah....
You can then put the array in a js file, include the file in all necessary documents, and access the array from there. This will provide you with a one-page source for editing purposes.
Vinny
Where the World Once Stood
the blades of grass
cut me still
-
Nov 16, 2000, 23:09 #6
- Join Date
- Aug 1999
- Location
- Lancaster, Ca. USA
- Posts
- 12,305
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Thanks for a good quick beginning tutorial on parameters and creating re-usable code. You covered the basics of both in a few short sentences.
What he didn't mention is you can use the same techniques mentioned above in any programming language available today.This will prevent a lot of wheel re-inventing
-
Nov 16, 2000, 23:24 #7
- Join Date
- Aug 1999
- Location
- Pittsburgh, PA, USA
- Posts
- 3,910
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Absolutely. I'm trying to included common files and things like that whenever possible - still having a *VERY* hard time grasping OOP though - I'm searching for decent tutorials on the subject.
Hopefully, when OOP "clicks" for me, I'll be able to take a major leap forward and get into some more advanced programming. Sure is tough, though! It's annoying to know the basics of several languages - just enough to write some fairly useful things, but not enough to say I really "know" the language.
-
Nov 17, 2000, 16:22 #8
- Join Date
- Oct 2000
- Location
- Nashvegas Baby!
- Posts
- 7,845
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
AMEN Chris...well said. I struggle with that too as I "know" Javascript and am learning PHP. It's hard to get past the basic level.
Adobe Certified Coldfusion MX 7 Developer
Adobe Certified Advanced Coldfusion MX Developer
My Blog (new) | My Family | My Freelance | My Recipes
-
Nov 18, 2000, 00:41 #9
- Join Date
- Oct 2000
- Posts
- 304
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Just hang in there -- the OOP will come... Spend some time being certain to understand the key concepts -- objects, properties, methods -- and what *exactly* they are.
This crucial step is where many get bogged down, especially those used to older languages that are completely sequential.
-
Nov 20, 2000, 21:12 #10
- Join Date
- Feb 2000
- Location
- where the World once stood
- Posts
- 700
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi,
OOPs, or what I don't know, helps
If you want to start learning OOP, take all of your functions/scripts and start generalizing/un-hardcoding them. That is, find a function and remove any direct mention of a value, object name, etc. If possible, functions should be as totally ignorant of what is being passed to them as possible.
For example, most people would calculate a geometric shape's area by creating a function for each shape (calcCircle, calcRectangle, etc). To paraphrase Shakespeare, though, an area under any shape is still an area. Now there are many ways to do this, but the following in one (another if I remember right is at my site in the "Arguments Property" tutorial.)
function calcArea()
{
var area = 0;
switch (arguments.length)
{
case 2:
if (arguments[0] == 'c') // it's a circle
area = arguments[1] * 3.14;
else // it's a square
area = arguments[1] * arguments[1];
break;
case 3:
area = arguments[1] * arguments[2];
break;
default:
area = ....errorCode goes here...
}
return (area);
}
Vinny
Where the World Once Stood
the blades of grass
cut me still
Bookmarks