473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

passing var from <head> to <body>


<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>
<body>
<script>
document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass it from function to here?
</script>
</body>

this is for dynamic content.. what prints depens on what item user
selects in sel obj.. thank you..
Sep 19 '05 #1
15 2201

"Frances" <fd***@yahoo.com> wrote in message
news:43*********************@news.sunsite.dk...

<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>
<body>
<script>
document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass it from function to here?
</script>
</body>

this is for dynamic content.. what prints depens on what item user selects
in sel obj.. thank you..


if the content is dynamic you will need to create a function that creates
the text and an event to trigger it. for example the onChange event of your
select could call a function call writeDynamicContent() which then spits out
the text in the <div>.
Sep 19 '05 #2
Zoe Brown wrote:
"Frances" <fd***@yahoo.com> wrote in message
news:43*********************@news.sunsite.dk...
<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>
<body>
<script>
document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass it from function to here?
</script>
</body>

this is for dynamic content.. what prints depens on what item user selects
in sel obj.. thank you..

if the content is dynamic you will need to create a function that creates
the text and an event to trigger it. for example the onChange event of your
select could call a function call writeDynamicContent() which then spits out
the text in the <div>.

yes, I'm calling doIt() (function that writes dynamic content) with
onChange Event handler in sel obj.. but still content doesn't print
where I want it to b/c var isn't being passed from function to where I
want content to print... how DO you pass a variable from inside a
function to outside it? thank you very much for yr help...
Sep 19 '05 #3
Frances wrote:

<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>

list = document.forms[0].product; // Now "list" is global.

Mick

[snip]
Sep 19 '05 #4
Lee
Frances said:


<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>
<body>
<script>
document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass it from function to here?
</script>
</body>

this is for dynamic content.. what prints depens on what item user
selects in sel obj.. thank you..


It's not really a matter of visibility between the head
and body. You've declared the variable selItem to be
local to function doIt().

Removing the "var" keyword from the declaration will make
the variable global.

Sep 19 '05 #5
Lee <RE**************@cox.net> wrote:
Removing the "var" keyword from the declaration will make
the variable global.


My personal preference is to explicitly declare global variables as
such.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Sep 19 '05 #6
Mick White wrote:
Frances wrote:

<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>

list = document.forms[0].product; // Now "list" is global.

and where is this var declaration? if I put it outside function then
function can't read it.. if I put it inside function then it can't be
read outside function.. what am I missing here? how do you OUTSIDE
functions var's you have processed inside functions? thank you very
much.. again here is situation:

<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>
<body>
<select name="product" onChange="doIt()"> ... </select>
[...............]

<script>
document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass var from function to here?
</script>
</body>
Sep 19 '05 #7
Lee
Frances said:

Mick White wrote:
Frances wrote:

<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>

list = document.forms[0].product; // Now "list" is global.

and where is this var declaration? if I put it outside function then
function can't read it..


Where did you get that idea? A var declared outside of any function
is global. You can also simply leave off the "var" keyword.

Sep 19 '05 #8
Lee
Christopher Benson-Manica said:

Lee <RE**************@cox.net> wrote:
Removing the "var" keyword from the declaration will make
the variable global.


My personal preference is to explicitly declare global variables as
such.


That would be nice, if there was a way to do so in Javascript.

Sep 19 '05 #9
Lee wrote:
Frances said:

<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>
<body>
<script>
document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass it from function to here?
</script>
</body>

this is for dynamic content.. what prints depens on what item user
selects in sel obj.. thank you..

It's not really a matter of visibility between the head
and body. You've declared the variable selItem to be
local to function doIt().

Removing the "var" keyword from the declaration will make
the variable global.


oh gosh, I see, it's like java then.....:)

so this will make it global:

var list;
var selItem;
function doIt() {
list = document.forms[0].product;
selItem = list.options[list.selectedIndex].value;
}

thank you all very much for your responses......

Sep 19 '05 #10
Frances said the following on 9/19/2005 1:30 PM:
Lee wrote:
Frances said:

<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>
<body>
<script>
document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass it from function to here?
</script>
</body>

this is for dynamic content.. what prints depens on what item user
selects in sel obj.. thank you..


It's not really a matter of visibility between the head
and body. You've declared the variable selItem to be
local to function doIt().

Removing the "var" keyword from the declaration will make
the variable global.


oh gosh, I see, it's like java then.....:)

so this will make it global:

var list;
var selItem;
function doIt() {
list = document.forms[0].product;
selItem = list.options[list.selectedIndex].value;
}

thank you all very much for your responses......


This makes it global also:

list = '';
selItem = '';

But your problem seems to be that you are trying to access a variable
that is inside a function but you are trying to access it before the
functions is executed. That means the variable has not been defined, nor
created yet, so you get the error.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 19 '05 #11
Lee <RE**************@cox.net> wrote:
That would be nice, if there was a way to do so in Javascript.


<html><script>
var foo;
</script></html>

What is the scope of foo?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Sep 19 '05 #12
Lee
Christopher Benson-Manica said:

Lee <RE**************@cox.net> wrote:
That would be nice, if there was a way to do so in Javascript.


<html><script>
var foo;
</script></html>

What is the scope of foo?


It's quite a stretch to call that "declaring it as global".
There is nothing about the statement that makes it global,
simply the fact that the statement appears in a global scope.

In production code, I find it best to simply name globals
appropriately, regardless of where they are first used:

function popup() {
globalPopupHTML="<html><body><p>Hello, world!</p></body></html>";
...
}

Sep 19 '05 #13
Lee <RE**************@cox.net> wrote:
It's quite a stretch to call that "declaring it as global".
There is nothing about the statement that makes it global,
simply the fact that the statement appears in a global scope.
That's all I really intended to imply. Maybe

window['foo']='foo';

?
In production code, I find it best to simply name globals
appropriately, regardless of where they are first used: function popup() {
globalPopupHTML="<html><body><p>Hello, world!</p></body></html>";
...
}


I'd probably name it gPopupHTML, if I were to take that route.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Sep 19 '05 #14

Frances wrote:
<html>
<head>
<script> Don't forget to include the type attribute:

<script type = "text/javascript">
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
Have the selItem as a global variable:

var selItem = "";

function doIt()
{
...code statements...
selItem = list.option[list.selectedIndex].value;
...code statements...
}

Don't forget to have a closing script tag:

</script>
</head>
<body>
<script>
<script type = "text/javascript">
document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass it from function to here?
Now you can use the selItem variable here since it was declared global.
</script>
</body>

this is for dynamic content.. what prints depens on what item user
selects in sel obj.. thank you..


Sep 19 '05 #15
web.dev said the following on 9/19/2005 7:31 PM:
Frances wrote:
<html>
<head>
<script>


Don't forget to include the type attribute:

<script type = "text/javascript">
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^

Have the selItem as a global variable:

var selItem = "";

function doIt()
{
...code statements...
selItem = list.option[list.selectedIndex].value;
...code statements...
}

Don't forget to have a closing script tag:

</script>
</head>
<body>
<script>

<script type = "text/javascript">
document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass it from function to here?

Now you can use the selItem variable here since it was declared global.


But it will blank, as you defined it as "". The desired (I am assuming)
effect is to have the list.option[list.selectedIndex].value placed where
they have the document.write statement.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 19 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andrés Giraldo | last post by:
Hi! It's possible to register some script between the <head> and </head> actually I'm Using RegisterStartupScript and RegisterClientBlockScript, but it works on the body By the way... how...
7
by: Ignac Vucko | last post by:
Is writing a document *during* page load safe and supported for all 4th and 5th generation browsers? If not, can you show me a specific example/browser where it causes problems? <html> <head>...
3
by: francescomoi | last post by:
Hi. I'm trying to insert some text between <head> and <body> but I'm not able. I try with: -------- bodyPoint = document.getElementsByTagName('body'); var myLink =...
10
by: Brian W | last post by:
Hi All, I have a web user control that, among other things, provides Print this page, and Email this page functionality I have this script that is to execute on the click of the asp:hyperlinks ...
5
by: Arne | last post by:
I need literal controls in the <head> section of my page. I need to be able to load dynamic values in the head section. It works fine in ASP.Net 1.1, but Asp.net 2.0 gives a compile error. I need...
1
by: Danny Scaleno | last post by:
Hello, using IEC Controller, anybody knows how to capture the head part of an html page like this one? <html> <head> <script language='javascript'>...
3
by: phpmel | last post by:
Hi guys, I have yet another question. I am working with this html form that uses a template. <head> //is greyed out //some greyed out <style >stuff is next <!-- InstanceEndEditable...
23
by: Xah | last post by:
Here's a interesting case of invalid html 4 strict file. In summary, if you have <body></bodywithout any content, the file would be invalid under html 4 strict. Valid if html 4 lose. <!DOCTYPE...
3
by: PYG | last post by:
Hi everybody I have a simple question : If i use this code : <body style="font-size:24px;color:blue;"> Text in body <table> <tr><td> Text in table
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.