6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

簡単なセッション保持

Last updated at Posted at 2016-02-03

location.href=URL で飛ばす時に値もっていきたい事は多々ありますよね

hiddenをごそっと

function getval(){
	var d = new Date();
	var a = ["_gt=" + d.getTime()];
	var input = document.getElementsByTagName('input');
	for(var i=0; i < input.length; i++){
		if(input[i].type != 'hidden') continue;
		a.push(input[i].name +'='+ input[i].value);
	}
	console.log(a.join('&'));
	return a.join('&');
}

location.href='?' + getval();

時間を貼りつけるのはキャッシュ防止です。

function getval2(keys){
	var d = new Date();
	var a = ["_gt=" + d.getTime()];
	for(var i=0; i < keys.length; i++){
		var e = document.getElementsByName(keys[i]);
		a.push(e[0].name+'='+e[0].value);
	}
	console.log(a.join('&'));
	return a.join('&');
}

location.href='?' + getval2(['a', 'b', 'c']);

必要なとこだけください。

document.getElementsByName()は配列で来るのでe[0]を取るのです。

6
5
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?