Reputation: 13
I have been asked by my boss to create a snippet to return a specific cookie (session ID) value to a test page on our company website. Problem is, I am a graphic designer with minimal coding experience! I foolishly showed a little HTML and CSS knowledge a year ago when our back end developer left the company, and we are currently in-between javascript people now so I've been conscripted to work on this.
I've spent the last hour or two so going through answers here on many many similar questions - and looking through various tutorials on W3 and other locations, I've learned how to use GetCookie, I've learned how to use document.write and I've learned how to use innerHTML but I'm finding that my general js understanding is just too limited to be able to convert more sophisticated examples into the specific result I'm trying to get. Basically, I can't figure out how to mash the two basic actions together.
This:
<script type="text/javascript">
document.write(getCookie("SessionID"));
</script>
Does exactly what I need, but I would like to make it return that cookie value to a specific div using innerHTML instead of rewriting the entire doc's HTML. Can someone show me how best to accomplish that? Thank you in advance, any guidance would be much appreciated!
Upvotes: 1
Views: 1226
Reputation: 9782
This should do it ...
<div id="cookie-jar">
</div>
<script>
document.getElementById("cookie-jar").textContent = getCookie("SessionID");
</script>
As a general practice you should avoid using innerHTML as it may allow injection of harmful content into pages.
Upvotes: 2