Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing JS variable to ASP page...

Status
Not open for further replies.

MarkRadaba

Technical User
Aug 26, 2004
6
US
How can I pass a variable in JavaScript to a variable in ASP? For example, in JavaScript, I'm setting a variable to screen.width.... how can I then pass that value to a variable in ASP?

-Mark
 
As dwarfthrower has demonstrated, you would pass the variable as if it were from a form submitted using the get method. In the same way, you can collect the data server-side as if they were submitted from a form. This technique is popular but be aware that it exposes the data (visually) to the end user in the URL.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Alternatively, you can create an invisible form (method="put") with hidden fields containing the value(s) you want to pass, and submit the form via javascript. That method doesn't expose the data visually like Jeff mentioned. It also allows you to pass much more data than the get method does.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
OOPS! [blush] I get that wrong all the time! Never could figure why they used get and post. Seems to me that get and put work better.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Here's what I'm trying to do, if it'll help everyone. I'm trying to determine how big a graphic needs to be in order to fill a particular space. Now, by using JS I'll check the width of the user's screen, which I would like to pass to an ASP page, which will then do the calculation as to the necessary size of the graphic. If possible, I'd like to do this in one page, and it all has to be automatic (ie, no form where they have to click, etc).

-Mark
 
ASP stuff is run server-side... whilst (I am picking) your javascript is run on the page client-side. To pass a variable from javascript to ASP (client-side to server-side) will require you either do a form submission, a page reload, or use AJAX techniques.

From your description now, I would be looking at the AJAX solution (google: AJAX example).

Having said that, you can get the width and height of an image using just javascript using...

Code:
<html>
<head>
<script type="text/javascript">
function test() {
	var imgObj = document.getElementById('testImg');
	alert(imgObj.width);
	alert(imgObj.height);
}
onload = test;
</script>
</head>
<body>
<img id="testImg" src="example.gif"/>
</body>
</html>

Which I have tested in windows on the following Opera 6+, Netscape 7+, IE 5.01+, Moz 1.7+, FF

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
That script tells me the size of the picture, which I already know... I need to be get the screen size so I can resize the picture appropriately.

For example, the pic is 640x480. The person's screen is 800px wide, but there are 2 columns that need to be accounted for, say 200px each. So I need to pass the 800px to an ASP equation that basically does 800-200-200=400, and then calls the picture, using the IMG tag with a width of 400.

I need to use the IMG tag because it calls another script which rotates the graphic, and it looks at the size attributes of the IMG tag in order to know what size to make the graphics. (Another option is to update the JS script that rotates the graphic get the size variable by doing the calculations itself - instead of the ASP page.)
 
How about this... Here's the script that runs a slideshow graphic. This is the graphic that needs to have the size adjusted based on the width of the screen. How can I modify this script to resize the image appropriately based on screen resolution?

<!--
// =======================================
// set the following variables
// =======================================

// Set slideShowSpeed (milliseconds)
var slideShowSpeed = 7500

// Duration of crossfade (seconds)
var crossFadeDuration = 3

// Specify the image files
var Pic = new Array() // don't touch this
// to add more images, just continue
// the pattern, adding to the array below

Pic[0] = 'images/anchor/newwelcome.jpg'
Pic[1] = 'images/anchor/graduationwewerethere.jpg'
Pic[2] = 'images/anchor/sportswewerethere.jpg'
Pic[3] = 'images/anchor/paradewewerethere.jpg'
Pic[4] = 'images/anchor/bill.jpg'


// =======================================
// do not edit anything below this line
// =======================================

var t
var j = 0
var p = Pic.length

var preLoad = new Array()
for (i = 0; i < p; i++){
preLoad = new Image()
preLoad.src = Pic
}

function runSlideShow(){
if (document.all){
document.images.SlideShow.style.filter="blendTrans(duration=2)"
document.images.SlideShow.style.filter="blendTrans(duration=crossFadeDuration)"
document.images.SlideShow.filters.blendTrans.Apply()
}
document.images.SlideShow.src = preLoad[j].src
if (document.all){
document.images.SlideShow.filters.blendTrans.Play()
}
j = j + 1
if (j > (p-1)) j=0
t = setTimeout('runSlideShow()', slideShowSpeed)
}
// End -->
 
I don't really know where you are going with this thread. You ask one thing, then another, and another... all very different questions. Maybe it's time to sit down and think things through a little more.

We're here to help others for specific problems that are preventing them from getting their work done... we are not your "project buddies". We all have jobs - and our own projects to work on [smile]

By all means come back with specific problems you are stuck on - but we are not your personal "code army" ready to modify random scripts on demand.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
However we do modify scripts randomly for our own amusement at times.

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
I haven't been asking for one thing and then another. My problem has always been trying to get a graphic resized based on the screen resolution.

It seems nobody understands what my problem is, which is why I have responded with posts to help clarify what I am trying to do.

The reason why I posted the javascript I have is with hopes that fellow programmers who have better experience with javascript can help me modify it in a way to do what I'm trying to do. I was basically giving two options of how I could possibly do it.

At no point was I asking for any "project buddies" to code the thing for me, but rather help me figure out a way for me to do it myself so I can get a better understanding of javascript.
 
MarkRadaba said:
My problem has always been trying to get a graphic resized based on the screen resolution.
It's quite easy to access the size of the browser window. The [tt]clientHeight[/tt] and [tt]clientWidth[/tt] properties of the [tt]body[/tt] will provide that information.

Using that, and your knowledge of your layout you can work out how big your image viewport is and work out how many pixels high and wide you need to scale to.

So if you ascertain that the available window area is 700 pixels wide by 500 pixels high and your image viewport is set to be 25% of the available screen you would get 700 * 0.25 = 175 pixels wide and 500 * 0.25 = 125 pixels high.

From there you would simply take the height and width of your images and work out whether scaling to 175 pixels wide or 125 pixels high would give you the best result (assuming you don't want to distort the image by changing both dimensions)

So if your image was 435 pixels wide and 200 pixels high:
(125 / 200) * 100 = 62.5% if you scaled on the height, which would put the width at 272 pixels - which is far too wide for your viewport. So:

(175 / 435) * 100 = 40.2% if you scaled on the width, which puts the height at 80 pixels which would fit easily.


[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top