PDA

View Full Version : Specify bookmark script



Bomu
04-07-2005, 10:44 PM
Hi there,

Not knowing anything about java scripts, I set off in search of a script on the web that I can use on my www.Domain/Frameset.html page which adds www.Domain/ to a user's bookmarks when they 'add to favorites' instead of adding www.Domain/Frameset.html

- make sense?

I've spent the last hour looking over search engine results, but can only seem to find variations of the "Bookmark this page" link script, - but I don't wanna add a image or text link :confused:

If anyone has a solution for this problem, I'd be eternally greatful.

cheers

mwinter
04-08-2005, 11:16 AM
make sense?Yes, but the answer is you can't. What a user bookmarks is outside of your control.

By the way, if you really want your visitors to be able to bookmark your pages, you should drop frames (you should drop frames anyway, but that's a different matter). Unless the user understands that they must bookmark the framed page, not the frameset, bookmarking is useless.

Mike

Bomu
04-08-2005, 04:58 PM
Thanks for the reply mwinter. I don't mind if people can't bookmark the framed content pages, I just want to the url they bookmark (if adding the Frameset to their Favorites) to be the index.hml at www.Domain/ rather than the Frameset.html within the website. I guess I can live without a bookmark url specifier, but it woulda been a nice touch if there was such a thing!

As for dropping the frames~ My website's structure and design at Organic Metal (http://www.organicmetal.co.uk) wouldn't work half as well without - not only do I need a top frame with an uninterrupted animated flash loop, but I use frames to allow people the option to view a second version of the site without the Flash loop and navigation, but still making use of the html content pages for both versions.

mwinter
04-09-2005, 10:55 PM
As for dropping the frames~ My website's structure and design at Organic Metal (http://www.organicmetal.co.uk) wouldn't work half as well withoutThe only thing that you couldn't keep with a completely frameless version is the background music as it would stop between page requests. Serving a Flash or Flash-less version is quite feasible as you have server side support. The layout is equally attainable. The question is if you want to make the effort of converting, which I doubt you do. However, if you ever redesign, you should keep frames out of the new version.

By the way, you might want to change your image fade code to only go to 99% opacity. Firefox has a peculiar bug which manifests itself upon reaching 100%. Someone asked about this on Usenet, so I provided a demo (http://mwinter.webhop.info/clj/yawnmoth/fade.html). I produced a revision (http://mwinter.webhop.info/clj/yawnmoth/fade-new.js) of the fade code to add support for additional user agents. If you decide to use any of it, use the revised code.

You can read the Usenet thread (http://groups.google.co.uk/groups?hl=en&lr=&threadm=opsim2f6e1x13kvk%40atlantis&rnum=1&prev=/groups%3Fq%3Dgroup:comp.lang.javascript%2Byawnmoth%2Bfade%26hl%3Den%26lr%3D%26selm%3Dopsim2f6e1x13kvk%2540atlantis%26rnum%3D1) via Google Groups.

Mike

Bomu
04-11-2005, 06:50 PM
Thanks again for your words of wisdom mwinter- Although it looks a little complicated, I'll look into modifying the fade script I'm using :)

I'll also be considering ditching the frames on the next re-design, but until then, do you know of a method I could use to force frames on content pages so that if a page is found outside of the frameset (i.e. via a search engine), a frameset will be automatically generated around that particular content page?

At the moment, I've implimented the following code to content pages:

if (window == top) top.location.href = "Domain/FrameSet.html";

But it only redirects, rather than displaying a framed version of the selected page :confused: ...any ideas?

jscheuer1
04-11-2005, 08:34 PM
if (window == top) top.location.href = "Domain/FrameSet.html";How about:

if (window == top) window.location.replace("Domain/FrameSet.html");That probably will not do all you want. You need to pass a variable to the frame telling it what content page to display within itself. See this thread:

http://www.dynamicdrive.com/forums/showthread.php?t=2237

Bomu
04-12-2005, 09:10 AM
jscheuer1~ Sounds like a plan. I checked out the thread link you had posted, but without a decent understanding of javascripting, I couldn't figure it out, not to mention how I could apply something like that to my FrameSet:

<html>
<head>
</head>
<frameset rows="160,*" cols="*" framespacing="0" frameborder="NO" border="0">
<frame src="frameTop.htm" name="topFrame" scrolling="NO" noresize>
<frameset rows="*,19" cols="*" framespacing="0" frameborder="NO" border="0">
<frameset rows="*" cols="157,*" framespacing="0" frameborder="NO" border="0">
<frame src="frameLeft.htm" name="leftFrame" frameborder="no" scrolling="no" noresize>
<frame src="../home.php" name="mainFrame" frameborder="no" scrolling="yes" noresize>
</frameset>
<frame src="frameBottom_LOADER.htm" name="bottomFrame" scrolling="NO" noresize>
</frameset>
</frameset>
<noframes>
<body>
No Frames Content
</body>
</noframes>
</html>

Any ideas would be greatly appreciated (providing a complete novice like me can understand them!)

jscheuer1
04-12-2005, 04:35 PM
I can see your problem, I am no expert at JavaScript either but, I figured this out ONLY works in IE6. Here is a template for your content page (I used your work.htm file as an example) that you want to invoke the frameset page with it (the content page) in the frame called 'mainFrame':
<html>
<head>
<title>Work</title>
<script type="text/JavaScript">
if (window == top) window.location.replace("http://www.organicmetal.co.uk/pages/index2.htm?var1=http://www.organicmetal.co.uk/pages/work.htm");
</script>
</head>
<body>
Work Content
</body>
</html>and your index2.htm could look like this:
<html>
<head>
<form name="recieve">
<input type="hidden" name="var1">
</form>
<script type="text/JavaScript">
if (document.all){
var locate = window.location;
document.recieve.var1.value = locate;
var text = document.recieve.var1.value;
function delineate(str) {
theleft = str.indexOf("=") + 1;
theright = str.indexOf("&");
return(str.substring(theleft, str.length));
}
url=delineate(text)
}
function refreshFrame() {
if (document.all){
if(window.location!=url)
frames['mainFrame'].window.location.replace(url);
}
}
</script>
</head>
<frameset rows="160,*" cols="*" framespacing="0" frameborder="NO" border="0" onload="refreshFrame()">
<frame src="frameTop.htm" name="topFrame" scrolling="NO" noresize>
<frameset rows="*,19" cols="*" framespacing="0" frameborder="NO" border="0">
<frameset rows="*" cols="157,*" framespacing="0" frameborder="NO" border="0">
<frame src="frameLeft.htm" name="leftFrame" frameborder="no" scrolling="no" noresize>
<frame src="../home.php" name="mainFrame" frameborder="no" scrolling="yes" noresize>
</frameset>
<frame src="frameBottom_LOADER.htm" name="bottomFrame" scrolling="NO" noresize>
</frameset>
</frameset>
<noframes>
<body>
No Frames Content
</body>
</noframes>
</html>Be sure to notice the onload event in the first framset tag.

jscheuer1
04-12-2005, 06:37 PM
OK, I've got one that works cross browser for NS7.2, FF1.0.2 and IE6. The sending page format is the same. On the receiving page, lose the form and use this script instead, so that the head section looks like this:
<head>
<script type="text/JavaScript">
var text = window.location.href;
function delineate(str) {
theleft = str.indexOf("=") + 1;
theright = str.indexOf("&");
return(str.substring(theleft, str.length));
}
url=delineate(text)

function refreshFrame() {
if(window.location!=url)
frames['mainFrame'].window.location.replace(url);
}
</script>
</head>Remember to still use the onload event in the first frameset.

Bomu
04-12-2005, 06:59 PM
Many thanks jscheuer1 - tested it out and it seems to do exactly what I wanted :)

I wonder if it would be possible to replace the url:
var1=http://www.organicmetal.co.uk/pages/work.htm
to a generic name such as:
var1=page.url

Reason I ask, is that I'd then be able to place this content page script within a JS and link that file from each content page, rather than pasting and edititing the script in each individual page.

Does that make sense?

but thanks again!

jscheuer1
04-12-2005, 08:35 PM
I was thinking about that but, I had to run. This script in the head of any sending page ought to put itself in the 'mainFrame' of the index2.htm:
<script type="text/JavaScript">
if (window == top) {
var url='window.location.replace("http://www.organicmetal.co.uk/pages/index2.htm?var1='+window.location.href+'")';
eval(url);
}
</script>assuming that you've configured index2.htm with the receiving script.

Bomu
04-12-2005, 09:43 PM
Excellent! I've put the script in an JS file and it's all working great- You may not consider yourself an expert- but as far as I'm concerned you are. Your assitance is totally appreciated :)

mwinter
04-12-2005, 11:11 PM
<head>
<form name="recieve">
<input type="hidden" name="var1">
</form>That would be horribly invalid, but luckly you acknowledge that the form isn't necessary. Out of interest, why did you include it?


<script type="text/JavaScript">Not that it really matters that much, but MIME types are all lowercase.


if (window == top) window.location.replace("http://www.organicmetal.co.uk/pages/index2.htm?var1=http://www.organicmetal.co.uk/pages/work.htm");I don't like that, mainly because the sub-URL needs to be encoded otherwise you've got an invalid URL.


function delineate(str)A better query string parser would be regular expression-based:


function fromQuery(name) {
var re = new RegExp('/(?|&)' + name + '=?([^&]*)/'),
m;

if((m = re.exec(document.URL))) {return decodeURIComponent(m[2]);}
}
eval(url);Oh, &deity; no! 99.9% of eval usage is incorrect, and this is certainly incorrect. The eval function is meant for the evaluation of a truly arbitrary string. It is slow and can lead to error-prone code, so only use it when it is necessary. In your case, a string concatenation is all that's required.

To check if re-framing is necessary:


if(('function' != typeof this.encodeURIComponent)
&& ('function' == typeof this.escape)) {
this.encodeURIComponent = function(c) {
return escape(c).replace(/@/g, '%40').replace(/\+/g, '%2B').replace(/\//g, '%2F');
};
}
if('function' == typeof this.encodeURIComponent) {
if((this == top) && this.location) {
var url = '/pages/index2.htm?frame=' + encodeURIComponent(document.URL);

if(location.replace) {
location.replace(url);
} else {
location.href = url;
}
}
}Using the fromQuery function above, you can then alter the frame:


if(('function' != typeof this.decodeURIComponent)
&& ('function' == typeof this.unescape)) {
this.decodeURIComponent = function(c) {
return unescape(c);
};
}

function setSource() {var frame, url;
if(('function' == typeof this.decodeURIComponent)
&& (url = fromQuery('frame')) && this.frames && (frame = frames['mainFrame']) && frame.location)
{
if(frame.location) {
frame.location.replace(url);
} else {
frame.location.href = url;
}
}
}Untested.

Mike

jscheuer1
04-13-2005, 01:48 AM
You lost me at the end there Mike. To answer at least one question, the one about the form - I used it because it is what I had to work with. I code JavaScript much the way I code markup. If it works in one place, I figure it might in another. I've used the form method of catching the variable from another page for quite some time on pages not involving frames, or at least not on the frameset page. I could see almost right away that it was not likely to work here but, lo and behold, it did but - only in IE6.

Added Later:

I tested your code and it switched to the page in question but did not replace the frame content.

Oh and I have a question for you. Other than it not working, which clearly is not the case with my code, what makes its output an invalid url?

mwinter
04-13-2005, 11:51 AM
[About the form] I could see almost right away that it was not likely to work here but, lo and behold, it did but - only in IE6.Bizarre, because it shouldn't. Oh well, doesn't matter.


I tested your code and it switched to the page in question but did not replace the frame content.There was a syntax error in the regular expression pattern used in fromQuery. It should have been:


function fromQuery(name) {
var re = new RegExp('(\\?|&)' + name + '=?([^&]*)'),
m;

if((m = re.exec(document.URL))) {return decodeURIComponent(m[2]);}
}I also forgot to mention that setSource was a replacement for refreshFrame, so it needs to be called on load. In the previously posted code, the query string value would be "frame=...", though that could be changed by altering the fromQuery call.


what makes its output an invalid url?URLs have a defined syntax, much like everything else. Consider the following URL.

http://www.example.com/index.php?url=/path/show.php?gallery=1&id=12

What would you make of that? Is id supposed to be part of url, or the original query string? It's difficult to say. That's why you must encode values that are added to the query string so that special characters cannot be misinterpreted. The answer, whether intended or not, is that id would be part of the original query string.

The example you used (work.htm) is dubious, but not likely to cause serious problems. However, your generic suggestion might as you don't know what location.href might yield. It could be something just as innocuous, or it could be something that may not be parsed properly. It depends on what URLs might be reframed.

Mike

jscheuer1
04-13-2005, 04:12 PM
[about form]Bizarre, because it shouldn't. Oh well, doesn't matterYeah, IE accepted it in the head, go figure . . .

OK, updated the query function, have yet to test though. However, since my last post I found that your sending code worked with my receiving code if I merely unescaped the incoming value before applying my code to it.


forgot to mention that setSource was a replacement for refreshFrameI figured that out.

Now back to the invalid URL's. I had been thinking along those lines to begin with but, when I tested on the OP's likely use, all was well (and it did work for the OP) so I didn't bother with it. My thinking was more along the lines of - You never see stuff like that in a passed variable, it always has those % things in it. Anyways, rather than test for encodeURIComponent, why not just escape and later unescape the address? I also tried a more circuitous method:
if (window == top) {
var outValue='';
for (i=0; i<window.location.href.length; i++)
outValue += '%' + window.location.href.charCodeAt(i).toString(16);
var url='window.location.replace("rec1.htm?var1='+outValue+'")';
eval(url);
}and then just unescaping that on the receiving page. Ugly looking, but, effective.

John

P.S. I know it still has that eval in it, just haven't gotten to that yet.

mwinter
04-14-2005, 12:54 PM
OK, updated the query function, have yet to test though. I did and found no problems.



forgot to mention that setSource was a replacement for refreshFrameI figured that out.I thought you would, but I still meant to point it out.


Anyways, rather than test for encodeURIComponent, why not just escape and later unescape the address?The encodeURIComponent and decodeURIComponent functions are specifically designed to deal with text that is to become part of a URI, whereas the deprecated escape and unescape functions, along with the up-to-date counterparts of encodeURI and decodeURI, deal with entire URIs so they aren't as suitable. As for total escaping (if that's what you're referring to), well that would bulk up the URL unnecessarily.



for (i=0; i<window.location.href.length; i++)
outValue += '%' +
window.location.href.charCodeAt(i).toString(16);There are a few potential issues with that, though one (maybe two) is fairly irrelevant.


The first sixteen ASCII control characters would be encoded as single character hexadecimal sequences, which would cause difficulty when decoding. They should be zero-padded to two characters.
According to Microsoft's documentation, the String.prototype.charCodeAt method wasn't implemented until JScript 5.5 (so typically, IE5.5+). That said, I do have IE4 and 5 on this machine and they do both implement the method, but they aren't standalone (that might account for the descrepancy). I used unescape as a quick hack, but I do have a better, though larger, implementation which emulates charCodeAt for code points 0-255.
Not using a native method when available would result in lower performance. I always check for built-in method support, and emulate only when necessary.




var url='window.location.replace("rec1.htm?var1='+outValue+'")';
eval(url);
window.location.replace('rec1.htm?var1=' + outValue);See? No eval necessary at all.


JohnAhh! So that's your first name. :D Now that's been revealed, do you mind if I ask how you'd prefer to be addressed? jscheuer (with or without the '1') is clumsy, but I'm OK with that if you don't prefer a familiar term.

Mike

jscheuer1
04-14-2005, 04:40 PM
Thanks Mike,

I actually got all of that.

John is fine, I just used jscheuer1 as it was in my autocomplete being my email address prefix. I believe many members of these forums are similarly named.

John