HTML and working with scripts

TappedOut forum

Posted on April 27, 2015, 9:21 p.m. by enpc

So recently I have been playing with javascript in HTML and have put together the following:

<h3>Table of Contents</h3>
<a id="bookmarker" onclick='gotoMarker()'>Go to Bookmark</a><br/>
<a HREF="#M1">Heading 1</a><br/>
<a HREF="#M2">Heading 2</a><br/>
<a HREF="#M3">Heading 3</a><br/>

<h3><a NAME="M1">Heading 1 </a><a id="M1_click" onclick='setCookie("location", "M1", 60)'>+</a></h3>
<p>text goes here</p>
<h3><a NAME="M2">Heading 2 </a><a id="M2_click" onclick='setCookie("location", "M2", 60)'>+</a></h3>
<p>text goes here</p>
<h3><a NAME="M3">Heading 3 </a><a id="M3_click" onclick='setCookie("location", "M3", 60)'>+</a></h3>
<p>text goes here</p>

<script>
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays2460601000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
//window.alert("cookie set - " + cvalue);
}

function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}

function gotoMarker() {
var marker = getCookie("location");
if (marker != ""){
window.location.hash = marker;
}
}
</script>
basically, this script gives your document a (manually built) table of contents with page jumps (still working on getting the landing exactly right). But beside each heading it adds a + symbol which is designed to bookmark that location. At the top of the table of contents, there is a "Go to Bookmark" button which will then launch you to the last saved bookmark.

Except I'm having trouble getting scripts to work. I have them working in the w3schools script editor but the code won't work here. If anybody could offer any direction at all that would be great.

enpc says... #2

yeaGO - this was the script I was talking about.

April 27, 2015 9:23 p.m.

filledelanuit says... #3

In general it isn't a good idea to declare functions inside of body tags, it can often confuse browsers and it may not work.

April 27, 2015 9:28 p.m.

enpc says... #4

But when this is going in a deck description how would you declare it outside of the body?

April 27, 2015 9:31 p.m.

filledelanuit says... #5

I'm not saying that you shouldn't but you could just replace everywhere you have declared the function with the actual code of the function to see if that is the problem. It is quite possible that there is a different but you can troubleshoot. I haven't had the opportunity to take a complete look but I can try to help.

April 27, 2015 9:40 p.m.

enpc says... #6

That could be one option yeah. I have been having a look, I miay be able to host hte code from a dropbox account and create an external link to it. Also for troubleshooting, the following line:

d.setTime(d.getTime() + (exdays2460601000));

actually should read:

d.setTime(d.getTime() + (exdays*24*60*60*1000));

April 27, 2015 9:46 p.m.

filledelanuit says... #7

I don't know how well that will work. Some browsers will not load javascript files that are from a different server as a security measure. I don't know what browsers do this but just be aware.

April 27, 2015 9:53 p.m.

This discussion has been closed