2016-07-05 7 views
0

변수의 문자열을 쿠키에 넣고 싶습니다. (내용으로) 현재 쿠키가 설정되고 이름은 username입니다. (그 부분은 작동합니다) 변수의 내용은 쿠키에 저장되지 않습니다. 여기에 내가 가진 것이있다. 나는 비슷한 질문을 한 번 보았고 그 대답은 그다지 효과가 없었다. 내가 문자열이다시피변수를 쿠키에 넣는 방법은 무엇입니까?

var sharedContent = sharedURL; 
var sharedURL = "http://www.example.com/"; 
function getUrl() { 
window.location.href= sharedURL; 
createCookie(); 
} 
function createCookie() { 
document.cookie = "username="+sharedContent+"; expires=Wed, 1 Jan 2025 13:47:11 UTC; path=/" 
} 

을 사용하고 가능하기 때문에, 내가 JS를 사용하여 조금 새로운입니다.

HTML :

<link href='https://fonts.googleapis.com/css?family=Lato:300' rel='stylesheet' type='text/css'> 

<a href="https://www.minds.com/newsfeed"><div class="button" onclick="getUrl(); "><img src="icon.png" width="16">Share</div></a> 
+1

,에 변화하고있다. 변수 정의는 게양되지 않습니다. 따라서 sharedURL은 첫 번째 var와 두 번째 sharedContent 예제 여야합니다. https://jsbin.com/zomewucomo/edit?html,js,console –

답변

1

그것은 당신이 변수/함수 선언 위치를 변경하는 경우 완벽하게 잘 작동하고 당신은 주변 선언의 주문을 전환 할 필요가 ;

var sharedURL = "http://www.example.com/"; 
var sharedContent = sharedURL; 
function createCookie() { 
var cookies = "username="+sharedContent+", expires=Wed, 1 Jan 2025 13:47:11 UTC, path=/" 
document.cookie = cookies; 
} 
function getUrl() { 
window.location.href= sharedURL; 
createCookie(); 
} 
getUrl(); 
+0

getURL(); 결국 거기에 있을까요? – user6333114

+0

getUrl() 함수를 호출하지 않아도 필요하지 않지만 getUrl() 함수 내부에 코드가 있으므로 쿠키를 설정할 수 없습니다. –

+0

테스트 해 보셨습니까? 나는 그 페이지를로드 할 때 example.com으로 리다이렉트했다. – user6333114

0

전화 window.location.href 전에 CreaetCookie() 기능. 나는 시도하지는 않았지만 효과가있다.

+0

테스트했을 때 작동하지 않았습니다. – user6333114

관련 문제