PDA

View Full Version : Java Script function won't come up in <iframe> Goal: Make DropDown remember selection



YashuaCastle
11-29-2007, 06:06 AM
Hi guys,... and thanks for your help in advance.

I've been searching for a week and tried about a hundred different scripts and they are all close, but none of them have a full example of what I'm looking for.

In short this is what I'm working on.
(The first two steps are working fine)
1. I have a drop down menu (a .php script) that generates different blog urls.
http://www.ifind2.com/WordPress/?cat=1
http://www.ifind2.com/WordPress/?cat=10
http://www.ifind2.com/WordPress/?cat=38
etc. (it's dynamic, and generated by users, so the url is always different).

2. This is inside an <iframe> on the main site http://www.ifind2.com.

3. My wish was originally the following: I wished that when a user selected their own blog from the drop down menu, next time they come back to the main site http://www.ifind2.com they would see their blog there automatically. This is done by a cookie that remembers their selection for 30 days.

I have two options for this java script.
Option 3.(a) when it's the visitor's first time at the site, and there is no cookie set yet, the java script defaults to the main category, which is http://www.ifind2.com/WordPress/?p=112.

3.(b) When a visitor makes a selection to their own blog, (or their favorite blog) from that drop down menu, the cookie is automatically set and the menu will always come up (for 30 days) with user's choice when they visit http://www.ifind2.com so the iframe would remember the url of the menu item they selected. Let's say: they selected http://www.ifind2.com/WordPress/?cat=38, then for 30 days that would come up on their PC in that iframe.

So my Java script generates a function called: SelectedURL()
This function works wonderfully getting the current page from a cookie.

I cannot assign it to an <iframe src=""> for the life of me.
Could anyone make it work? Would you please help me with this? Thanks.

Which choice is better? Making the dynamic .php drop down menu remember it's own selection, or having the <iframe> remember which page to start with? (that was my dilemma originally). I'd love to have some help with this. Thanks.

I have two options to make this work. One of my choices would be to have the drop down menu remember it's own selection with a cookie for 30 days once a selection was made by the user. My other choice would be, utilizing my own script below and assign the SelectedURL() Java Script function (which by the way shows the URL of the cookie set on a page, or the default url if there is no cookie set yet), but this java script function has to be assigned to the <iframe> which I cannot do for some reason. Again, thanks for your help.

Either solution would be greatly appreciated. Thanks a million.
Here is my current script below:


<html>

<head>
<script
LANGUAGE="JAVASCRIPT">
<!--//
var expdate = new Date ();
expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000*365)); // 1 yr from now
/* ####################### start set cookie ####################### */
function setCookie(name, value, expires, path, domain, secure) {
var thisCookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
document.cookie = thisCookie;
}
/* ####################### start show cookie ####################### */
function showCookie(){
alert(unescape(document.cookie));
}
/* ####################### start get cookie value ####################### */
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
/* ####################### end get cookie value ####################### */
}
/* ####################### start get cookie (name) ####################### */
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = "0";
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return 0;
}
/* ####################### end get cookie (name) ####################### */
/* ####################### start delete cookie ####################### */
function DeleteCookie (name,path,domain) {
if (GetCookie(name)) {
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-2009 00:00:01 GMT";
}
}
/* ####################### end of delete cookie ####################### */
function SelectedURL(){
var now= new Date();
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
var ifind2Blog = GetCookie("ifind2Blog");
setCookie("ifind2Blog", ifind2Blog, now);
if (ifind2Blog)
{
document.write(location.href);
setCookie("ifind2Blog", ifind2Blog, now);
}
else
document.write("http://www.ifind2.com/WordPress/?p=112");
}

function ChangeName(){
var ifind2Blog = GetCookie("ifind2Blog");
ifind2Blog = "top.iframe_name.location.href";
setCookie("ifind2Blog", ifind2Blog);

SelectedURL()}
// -->
</script>


</head>
<body>
<script language="JavaScript">
//-->
SelectedURL()
<!--//
</script>

<br>
<iframe name="ifind2BlogFrame" id="ifind2BlogFrame" src="">



</body>

</html>


Removed DD Script Posting Format, not needed for just normal questions. Also wrapped code in
tags.