8 Replies - 2585 Views - Last Post: 11 January 2011 - 09:16 AM

#1 ExEdzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 25-November 10

Javascript pass arguments to function

Posted 09 January 2011 - 12:14 PM

Hey guys.
Please help me.. I know in diferent languages (VB.Net,C++,C#) how to pass an argument to function in this code:

<html>
<head>
<script language="Javascript">
 <!-- hide
 function openNewWindow() {
 popupWin = window.open('http://webdesign.about.com/',
 'open_window',
 'menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=640, height=480, left=0, top=0')
 }
 // done hiding -->
 </script>
 </head>
<body>

<a href="javascript:openNewWindow();">return to front page</a>
</body>
</html>



i need to add several links in my page, so i need to pass indexes from links to main function, so i can open images in specific folder with indexes filenames..

For example in vb

Subname("text_to_pass_to_SubName")

Public Sub SubName(ByVal function_text as string)
End Sub

I hope you understand what i need..

Is This A Good Question/Topic? 0
  • +

Replies To: Javascript pass arguments to function

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3555
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: Javascript pass arguments to function

Posted 09 January 2011 - 12:20 PM

more or less. In Javascript you pass arguments/parameters like in any (loosely typed) C-style programming language.

the only noteable difference is that even if you don’t define parameters, you can still get the arguments, but that’s for some other time.

PS. you answered part of your question already in your example

This post has been edited by Dormilich: 09 January 2011 - 12:21 PM

Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,213
  • Joined: 07-September 06

Re: Javascript pass arguments to function

Posted 09 January 2011 - 01:06 PM

This may seem like a dumb question (and suggestion), however what are you using the popup for? If it is only to display the image there are a number of other things you could do.

The first (which would make your life easier) would be to use the target attribute of links to open a new window when it is clicked. Then you just treat it like a standard link and every time it is clicked it will open in either a new window or a new tab (depending on the browser and user settings).

Second, you could display the images in a modal window. See here: http://www.queness.c...dal-window.html for a few examples of different modal styles.
Was This Post Helpful? 0
  • +
  • -

#4 ExEdzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 25-November 10

Re: Javascript pass arguments to function

Posted 09 January 2011 - 01:48 PM

BetaWar

It seems that i rather need the image viewer, but my javascript level is not enough to program some viewer, that looks nice..

and Dormilich

I got the thing, but how can i use my example?

Something like this?

function(string_here){
document.write(string_here)
}


<a href="" onclick=function(string_for_string_here)>link text</a>


???

This post has been edited by Dormilich: 09 January 2011 - 02:16 PM

Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3555
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: Javascript pass arguments to function

Posted 09 January 2011 - 02:21 PM

that won’t work for a couple of other reasons.
- document.write() kills your page (including all scripts) when used after the page finished loading.
- strings must be enclosed in quotation marks (' or ")

you should read a tutorial on Javascript.

This post has been edited by Dormilich: 09 January 2011 - 02:22 PM

Was This Post Helpful? 0
  • +
  • -

#6 ExEdzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 25-November 10

Re: Javascript pass arguments to function

Posted 10 January 2011 - 04:55 AM

Sorry.. that wasnt the point..

i ment, was the parameter send to function correctly?

like this:


Function function_name(s) {

//now i can do something with "s" witch is integer 1000
}

< href="" onclick="function_name(1000)">text</a>



Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3555
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: Javascript pass arguments to function

Posted 10 January 2011 - 05:33 AM

if you write function instead of Function. (Function refers to the Function object, not the function statement)

PS. there are no integers in Javascript, only floats.

PPS. Javascript is case-sensitive
Was This Post Helpful? 0
  • +
  • -

#8 forest51690  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: Javascript pass arguments to function

Posted 10 January 2011 - 02:38 PM

View PostExEdzy, on 09 January 2011 - 12:48 PM, said:

<a href="" onclick=function(string_for_string_here)>link text</a>


Is this what you're looking for?
<a href="" onclick="functionName('string_for_string_here')">link text</a>


It will pass that string to the function named functionName
Was This Post Helpful? 1
  • +
  • -

#9 ExEdzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 25-November 10

Re: Javascript pass arguments to function

Posted 11 January 2011 - 09:16 AM

View Postforest51690, on 10 January 2011 - 01:38 PM, said:

View PostExEdzy, on 09 January 2011 - 12:48 PM, said:

<a href="" onclick=function(string_for_string_here)>link text</a>


Is this what you're looking for?
<a href="" onclick="functionName('string_for_string_here')">link text</a>


It will pass that string to the function named functionName


Thats exactly what i need.. tnx :)

SOLVED (completely)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1