Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 6th, 2006, 07:34 AM
Friend of Wrox
 
Join Date: Jun 2005
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.
 
Old July 7th, 2006, 10:06 AM
Friend of Wrox
 
Join Date: May 2004
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

 
Old July 12th, 2006, 05:37 AM
Friend of Wrox
 
Join Date: Jan 2006
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/
 
Old July 21st, 2006, 05:16 PM
Friend of Wrox
 
Join Date: Jun 2005
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!





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





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