Solved

Problem with location.href

Posted on 2009-04-13
5
265 Views
Last Modified: 2012-05-06
Hi Folks,

I am writing a cookie based bookmarking feature, so that users can return back from where they have left.

Here is my folder structure

index.htm // open the page to launch menu.htm in the window and then close
menu.htm // menu for all the lessons

Menu is a simple a href

<a href="overview/overview/index01.htm">overview</a>

This is the first line of code for index01.htm
<frameset rows="100%,*" frameborder="no" border="0" framespacing="no" onLoad="getBookmarkCD();" onbeforeunload="bookmarkit();">

<a href="au/au/index01.htm">Authorities</a>

so on, when the page closes I write the page number in a cookie. This works fine.

My problem is if the bookmark info is for the Overview lesson and the user clicks on the Authorities lesson, my path is not able to go to the overview lesson.

The cookie is looking for filename page5.htm in the overview folder. So when the user clicks on the Authorities folder it is still looking for page5.htm which is not in the Authorities folder but it is in the overview folder.

How do i resolve my path because with the code below my path is resolving to
au/au/page5.htm instead of

overview/overview/page5.htm when the overview link is clicked, it works fine when authorities hyperlink is clicked in the menu. The folder path only resolves to the hyperlink clicked.

How can this be changed within the existing code.

If I store the full path in the cookie like "overview/overview/page5.htm" and I click on Authorities, the path resolves to

au/au/overview/overview/page5.htm

Any help is greatly appreciated.

Thanks

RG

bookmark.js
 
function bookmarkit(){
	//alert("bookmarkit called");
	//tmout = setTimeout("gotoBkm()", 1000);
	
	if (Content.lastPage){
		set_cookie ( "bookmarkcd", "false", -1, -1, -1 );
	}else{
		setBookmark(Content.window.location);	
	}
	
}
 
 
 
function getBookmarkCD() {
		//var lastpage = WM_readCookie("bookmarkcd");
		var lastpage = WM_readCookie("bookmarkcd");
		//alert("lastpage > " + lastpage);
 
		// make sure we have a last page to return to.
		if ( lastpage == 'false' ) {
			//setBookmark( window.location );
			return;
		}
 
		// we have a bookmarked location
		
		if (lastpage){
			if ( confirmBookmark( lastpage ) ) {
				returnToBookmark( lastpage );
			} else {
				//setBookmark( window.location );
			}
		}	
	}
 
// confirm that you want to return to the last bookmarked page
function confirmBookmark( title ) {
 
        result = confirm('Do you wish to return to your last location (' + title + ')?');
 
        if ( result ) {
                return true;
        } else {
                return false;
        }
 
}
 
 
 
 
function getFilename ( this_location ) {
	
	// get path part of URL
	var this_pathname = this_location.pathname;
	
	// split on /
	var dirs = this_pathname.split('/');
	// filename is last element
	return dirs[dirs.length-1];	
}
 
function setBookmark( this_location ) {	
	var strNav = Navigation.count ;
	//alert("strNav > " + strNav);
	var filename = getFilename( this_location ); 
	//alert("filename > " + filename);
	//document.cookie = "bookmarkcd=" + filename; // Bookmark the filename into  a Cookie
	
	set_cookie ( "bookmarkcd", filename, 2020, 12, 15 );
	set_cookie ( "strNavCounter",  strNav, 2020, 12, 15 );
	
	//document.cookie = "strNavCounter=" + strNav; // Put the Navigation Counter Value into a Cookie
}
 
 
function returnToBookmark( lastpage ) {
	var counter =  WM_readCookie("strNavCounter");
 
	Navigation.count = counter ; 
	var this_location = window.location;
	
	var this_pathname = this_location.pathname;
	var new_pathname  = "";
	
	
	// split on /
	var dirs = this_pathname.split('/');
	// set filename (last value) to the lastpage we got
	dirs[dirs.length-1] = lastpage;
	
	for ( var i=0; i < dirs.length; i++ ) {
		new_pathname = new_pathname  + "/" + dirs[i];
	}
	// remove first character
	new_pathname = new_pathname.slice(1);
 
	
	// set the location parameter with the new pathname
	// and apparently jump to the page
	//this_location.pathname = new_pathname;
	
	var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
 
	if (ie7){
	    Content.location.href = lastpage;
	}else{
	    Content.location.href = new_pathname;
	}
}

Open in new window

0
Comment
Question by:rgarimella
  • 4
5 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 24135374
What is Content?
Also show WM_readCookie() function.
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 24135381
By the way, the answer to your question is simple: use path relative to web server root by prepending forward slash to the path: /
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 24135393
or in more detail:
1.) every href starting with http:// can select the server and is the absolute path.
2.) every href starting with / references the path from the current web server root path.
3.) anything else not starting with / or href: is relative to the curent web page folder path.
0
 

Author Comment

by:rgarimella
ID: 24136370
Content is the name of the Frame

<frameset rows="100%,*" frameborder="no" border="0" framespacing="no" onLoad="getBookmarkCD();" onbeforeunload="bookmarkit();">

<frame src="ab_03_01_0010.htm" name="Content" title="Main Content" SCROLLING="no" MARGINWIDTH="0" MARGINHEIGHT="0" FRAMEBORDER="no" BORDER="0" noresize>

<frame src="bottomnav01.htm" name="Navigation" title="Navigation" SCROLLING="no" MARGINWIDTH="0" MARGINHEIGHT="0" FRAMEBORDER="no" BORDER="0" noresize>

</frameset>

Also all this is going on a CD Rom instead of a webserver
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 24141446
So what is your full URL path in the browser for your CD?
And what is the content of your WM_readCookie() function?
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question