0

how can I add the / char at the end of the URL without force the user to write it? I'm starting to work with SWFAddress using JavaScript and jQuery directly (without Flash) like this wonderful site.

So something like this:

http://mysite.com/section

// and after click to a specific <a ="#">button</a> get
http://mysite.com/section/#/sub-content

// instead of
http://mysite.com/section#/sub-content

I've started with this code to do it:

$(".time_dot, .time_year").click (function () {
    onClickEvent (this);
});

function onClickEvent (dom) {
    var timer = setTimeout (function () {
        SWFAddress.setValue($(dom).parent().attr("id"));
    }, 200);
    // do something else 
}

The setTimeout method is used to avoid the <a> button overwrite the SWFAddress.setValue method, but I don't know a way to change the URL address to url.com/content#/... to url.com/content/#/....

How can I do that?

vitto
  • 19,094
  • 31
  • 91
  • 130
  • The URL is created by the btn id in this case, if I have Contacts I'll set http://mysite.com/section/#/contacts like the site Irrland I've posted. – vitto Dec 14 '10 at 20:46

4 Answers4

2

As already said, you cannot change the URL without forcing a reload of the site. And I don't think you want that. (but it depends on how SWFAddress.setValue() actually works, if it just keeps track of the URLs internally (without changing the browsers URL) then you can do it)

But I wanted to give you an alternative for setTimeout:

$(".time_dot, .time_year").click (function (event) {
    event.preventDefault();
    event.stopPropagation();
    onClickEvent (this);
});

function onClickEvent (dom) {
    SWFAddress.setValue($(dom).parent().attr("id"));
    // do something else 
}

With this you prevent the click event from bubbling up and also prevent the change the default action (which would be following the link).

See jQuery event object.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • it's great, thankyou, I've saw http://irrland.sonntagskunst.de/ doing it but I don't know how it does exactly. – vitto Dec 14 '10 at 20:56
  • @vittorio: Which browser are you using? The URL already ends with a `/`. If you use Chrome, this will be hidden, but Firefox shows it. Just type `irrland.sonntagskunst.de` in Firefox and see what happens... – Felix Kling Dec 14 '10 at 21:03
  • sorry, I've saw now in http://irrland.sonntagskunst.de/ only works if i don't change the url, so it can be done only if the url is allready set like thi http://irrland.sonntagskunst.de/ instead of http://irrland.sonntagskunst.de (without / then) – vitto Dec 14 '10 at 21:07
  • 1
    @vittorio: If you want to make URLs appear as you type them, you have to put them into backticks `\``. Like `\`http://example.org\`` – Felix Kling Dec 14 '10 at 21:08
1

You can't change the "path" part of the URL without reloading the page. So, if you're at http://mysite.com/section, you can not navigate to http://mysite.com/section/#/sub-content without reloading the page.

Mr. TA
  • 5,230
  • 1
  • 28
  • 35
  • It can be done with http://en.wikipedia.org/wiki/SWFAddress / http://www.asual.com/swfaddress/ and It's commonly used on Flash sites. I see the browser load for a millisecond but the page doesn't refresh. – vitto Dec 14 '10 at 20:51
  • @vittorio: The page you linked to says *SWFAddress uses the "hash" portion of the URL*. So everything **after** the `#` can be changed as you wish, but not before. If it really works then please give a link to website that does so. – Felix Kling Dec 14 '10 at 20:54
0

you mean like

$(".time_dot, .time_year").click (function () {
   setTimeout (function () {
         SWFAddress.setValue("/"+$(this).parent().attr("id")+"/");
    }, 200);

});
FatherStorm
  • 7,133
  • 1
  • 21
  • 27
0
function fixUrl(url) {  
    return url.replace(/\/?$/,'/');  
}

//or

function fixUrl(url) {  
    return url.match(/\/$/)  
       ? url  
       : url + '/';  
}
cslavoie
  • 216
  • 1
  • 6