Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 6th, 2006, 07:34 AM
Friend of Wrox
Points: 1,075, Level: 12
Points: 1,075, Level: 12 Points: 1,075, Level: 12 Points: 1,075, Level: 12
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2005
Location: , , United Kingdom.
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default Can variables be pulled out of functions

If i have a function such as this:

function song_check_song(song, split_song, split_index){
    var x=0
    document.write('<html><body>');
    document.write('There are ' + split_index + ' words<br>');
    for (x=0; x<split_index;x++) {
    document.write(split_song[x].charAt(0).toUpperCase());
    var y=1;
    for (y=1; y<split_song[x].length; y++){
    document.write(split_song[x].charAt(y).toLowerCase());
    }
    document.write('&nbsp');
    }

    document.write('<br><form><input type="button" onClick="javascript:document.location("N:\\Music Manager\\Music Centre.html");"></form>')
    document.write('</body></html>');
}

And say I want to pull out the end result of the for statment in bold in a variable format for later use, how would I go about soving this problem??

__________________
Apocolypse2005, I'm a programmer - of sorts.
Reply With Quote
  #2 (permalink)  
Old July 7th, 2006, 10:06 AM
Friend of Wrox
 
Join Date: May 2004
Location: , , .
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Apocolypse

You could store the variable's value in a hidden field in your document. Then you can reference the value of the hidden field later on.

So in your markup:

<input type = "hidden" id = "myHiddenField">

Then in your script:

document.getElementById("myHiddenField").value = myForLoopVar

HTH
Joe

Reply With Quote
  #3 (permalink)  
Old July 12th, 2006, 05:37 AM
Friend of Wrox
 
Join Date: Jan 2006
Location: San Francisco, CA, USA.
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What about pulling the bolded code out and putting it in its own function? Then you can either call that function from elsewhere in your code, or you could have a controller function (a 3rd function) that would call the bolded code function, store it as a Javascript variable (or as a hidden form field, as Joe/interrupt suggested), and then pass the bolded code as a parameter to your existing function.

With respect to Joe's suggestion, you could do the same thing more efficiently by simply defining a var inside a Javascript block (outside of a function declaration) and setting that var to the generated HTML. Your var would then be accessible to all Javascript functions by name. By using a hidden form field, accessibility would require looking up the data off of the form, and also complicating your DOM with unneccessary form fields. Both ways would work perfectly though :).

Jon Emerson
http://www.jonemerson.net/
Reply With Quote
  #4 (permalink)  
Old July 21st, 2006, 05:16 PM
Friend of Wrox
Points: 1,075, Level: 12
Points: 1,075, Level: 12 Points: 1,075, Level: 12 Points: 1,075, Level: 12
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2005
Location: , , United Kingdom.
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default

could you make a that code up and then incorporate it within the function so i could see how it is down please, tahnkyou very much for you suggestions!

------------------------------------------------
Apocolypse2005
Always ready and waiting to help!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning Automatic Variables from Functions jabney BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 2 November 29th, 2008 09:56 PM
Help with functions and variables DeadlyDesigns.NET Beginning PHP 6 January 26th, 2005 12:28 AM
Help needed with variables and functions interrupt Javascript How-To 2 May 24th, 2004 08:47 AM
Functions and variables collie VB.NET 2002/2003 Basics 3 February 5th, 2004 02:45 AM
Global variables and functions madhukp ASP.NET 1.x and 2.0 Application Design 3 October 9th, 2003 09:57 AM



All times are GMT -4. The time now is 03:24 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.