使用javascript将值传递到下一页 [英] Passing values to next page using javascript

查看:90
本文介绍了使用javascript将值传递到下一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我使用JavaScript传递值,但我不希望URL像这样

Inventory / Asset.aspx?= 4



网址应如下:Inventory / Asset.aspx





Hi
I am passing values using JavaScript, but I don't want URL to be like this
"Inventory/Asset.aspx?=4"

URL should be like: "Inventory/Asset.aspx"


function next(idx) {
    var aData = oTable.fnGetData(idx);
    window.location = '/Inventory/Asset.aspx?=' + aData["assetID"]
}

推荐答案

使用cookies:W3Schools JavaScript Cookies [ ^ ]
Use cookies: W3Schools JavaScript Cookies[^]


在没有查询字符串和cookie的情况下可以做的一件事。

这是会话变量。如何?

来自javascript你会调用ajax函数(ajax / web方法,这个方法应该允许会话访问)并且在那个函数/方法中你将该值存储到会话变量

Session [key] = value;

然后从其他页面读取该会话值。



此方法有但它有缺点。如果不从第2页的会话中删除变量,则无论您是否实际来自第1页,该页面的行为就像您在请求该URL时导航到特定的assetID一样。如果你删除变量,如果用户刷新页面,或者通过后退按钮导航到页面,页面将不会按预期运行(除非浏览器从缓存中获取页面并且不再请求它)。



HTTP是无状态的。为什么不使用提供的机制 - 即URL - 让浏览器说出它要求的内容?这比服务器记住用户导航的方式更容易,更可靠,特别是因为服务器可能会响应,但响应不一定会到达浏览器。



如果你真的不喜欢查询字符串,你可以使用不同的语法,例如mysite.com/assets/1010而不是mysite.com/assets?id=1010。这些日子这样做应该很流行,我想如果你搜索URL重写,我会发现你会发现大量的信息。
one thing you can do without querystring and cookie.
That is session variable. How?
from javascript you will call ajax functions(ajax/web method and this method should be allow session access) and with in that function/method you store that value to a session variable
Session["key"] = value;
then from other page you read that session value.

This method has its drawbacks though. If you don't remove the variable from session in page #2, the page will behave as if you navigated to the particular assetID whenever that URL is requested, regardless of whether you actually came from page #1 or not. And if you DO remove the variable, the page will NOT behave as expected if the user refreshes the page, or when navigating to it via the back button (unless of course the browser takes the page from cache and does not request it again).

HTTP is stateless. Why not just use the mechanism provided - i.e. the URL - to have the browser say what it's asking for? This is easier and more reliable than having the server "remember" how the user has been navigating, especially since the server may respond but that response isn't certain to reach the browser.

If you really don't like the querystring, you could use a different syntax, e.g. "mysite.com/assets/1010" instead of "mysite.com/assets?id=1010". Doing this is supposedly quite trendy these days, and I think you'll find tons of info on the how and why if you search for "URL rewriting".


这篇关于使用javascript将值传递到下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆