Jump to content

Alert On First Visit (solved!)


danman97

Recommended Posts

Hi,I have created an alert which tells people information when the page loads. However, I don't want it to load post-first visit as alerts can get annoying!Here is my code so far (its in the body):

<script language="Javascript">setTimeout("alert('Some passcode protected documents may not show on mobile devices such as the iPad. If your document does not load, you should ask for a file attachment instead.')",2500)</script>

Here is the page:meljones.info/downloads.htmlI was hoping there was a solution. Is cookies an option? I'm new to JavaScript...Also, I have no access to the HEAD of my page...Dan

Link to comment
Share on other sites

HiThanks for the replies but could not get them to work. However I did get the new Local Storage which is new with HTML5 working which I found surprisingly easy!! :)Heres the java script:

if (localStorage.pagecount)	{	localStorage.pagecount=Number(localStorage.pagecount) +1;	}else	{	localStorage.pagecount=1;	setTimeout(alert('Please Note, \nSome passcode protected documents may not show on mobile devices such as the iPad. If your document does not load, you should ask for a file attachment instead via e-mail. \nmail@meljones.info'),2000)	}

This shows an alert the first time a page is seen and then not again unless Local Storage is cleared. It is in a separate JavaScript document.See example: Working ExampleThanks!Ps. I took most of it from this article: W3Schools - HTML5 Web Storage

Link to comment
Share on other sites

Be aware that this technique will fail on versions of IE before 8, which don't support this kind of local storage. IE7 and IE6 still account for over 10% of all browsers in use. The alert won't ever show up in those because execution terminates as soon as you try to access localStorage.pagecount.Cookies take a little more scripting, but all browsers support them, and very few users disable them. The setCookie and getCookie functions on the W3Schools tutorial can be copied and pasted with no changes. They're actually very easy to use.Alternatively, older versions of IE have a special type of local storage. You could write some conditionals to use what's available. The old IE technique is kind of ugly, though.As a total fallback, simply test for the localStorage object. If it doesn't exist, do something else. The test is simply this:if (localStorage) // blahEven if localStorage does not exist, this test will not terminate execution. Testing for properties of a non-existent localStorage object will throw an error and terminate execution.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...