Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript 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 August 8th, 2007, 02:05 PM
Authorized User
 
Join Date: Aug 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem setting a variable

I have a strange problem where I can't set a variable or something else is going on.
here is a simple function I'm using

//the string I'm passing in is a url
//ie http://www.mywebsite.com/public/index.asp?x=2323

function validate(str){
alert(str); //this works
var strnew = str;
alert(strnew); //this works
alert("test" + strnew.length); //this doesn't work.I get an undefined

//but if I do something like this
var strnew1 = "http://www.mywebsite.com/public/index.asp?x=2323";
alert("test1" + strnew1.length); //this works
}

What am I missing here ?


 
Old August 9th, 2007, 02:42 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I get test48 alerted as expected, perhaps some other piece of code is affecting the output? What does alert(strnew.length) show? Is there a chance some code has removed the length method?

--

Joe (Microsoft MVP - XML)
 
Old August 9th, 2007, 04:22 AM
Authorized User
 
Join Date: Jul 2007
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vksingh24
Default

I got the same result test48.

Although, It seems like there is no problem with the code..

You can modified the code as following:

<HTML>
<HEAD>
<script language="JavaScript">

function validate(str){
alert(str); //this works
strnew =new String(str);
alert(strnew); //this works
alert("test" + strnew.length); //this doesn't work.I get an undefined

//but if I do something like this
//var strnew1 = "http://www.mywebsite.com/public/index.asp?x=2323";
//alert("test1" + strnew1.length); //this works
}

validate("http://www.mywebsite.com/public/index.asp?x=2323")
</script>
</HEAD>
</HTML>

__________________
Vikash Kumar Singh
 
Old August 9th, 2007, 08:03 AM
Authorized User
 
Join Date: Aug 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks,

I tried adding a hard coded value to the html page
validate("http://www.mywebsite.com/public/index.asp?x=2323")

The javascript worked fine when passing in the hard coded value.
I did some other testing and it seems that no other javascript functions will work either when a value is passed in as shown below.

In my original code on the html page I was using
validate(window.location)

So I guess the next question is why doesn't validate(window.location) work ?
 
Old August 9th, 2007, 08:36 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

window.location is an object, try window.location.href. If you'd told the real story at the beginning...

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
conditionally setting variable ewel Beginning PHP 2 December 13th, 2007 07:39 PM
Newbie : How do you .. without setting a variable rqaran XSLT 2 August 6th, 2005 04:09 AM
declare a variable without setting a value to it crmpicco Javascript How-To 1 July 18th, 2005 07:25 PM
setting each value from database as a variable Ashleek007 Beginning PHP 21 April 18th, 2005 03:50 PM
Setting a DTS Global Variable ioates SQL Server 2000 0 October 23rd, 2003 04:28 AM





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