0

This is a tough one to explain, but I'll try my best! I'm programming my app in JavaScript, and I'm using the Titanium IDE.

So, I'm trying to create a bookmarks menu for my app. I need it to essentially get the name/content header of the website, and that way the title of the bookmark will look decent, rather than just taking the direct route and cramming the Url of the website into the table.

To give an example of what I'm talking about,

Notice The Naming Of Each Bookmark

If you look, you can see that it says Apple, not http://www.apple.com or apple.com, and that's what I'm trying to replicate. If there's a function you might have or know a little about, please include it! Otherwise, any input is helpful!

Thanks

UPDATE: I found a solution if I was using XCode, and also for Titanium, for those that are interested. I know that document.title is the key here, but running it in Titanium, however, it's a little different. I'll post my conclusions below!.

Jtaylorapps
  • 5,680
  • 8
  • 40
  • 56

2 Answers2

1

It may not work for all sites, and maybe somone could improve it, but here is one solution in javascript:

var sitename = location.hostname;
sitename = sitename.replace("http://", "");
sitename = sitename.replace("https://", "");
sitename = sitename.replace("www.", "");
sitename = sitename.substring(0, sitename.indexOf("."));

In the solution above, we get the hostname, and then extract a substring from the beginning of the hostname to the first full stop. This way we can remove the .com part of the hostname.

We can't use sitename.lastIndexOf(); because then if we has a .co.uk domain, we would end up with something like example.co.

Hope this helps!

starbeamrainbowlabs
  • 5,692
  • 8
  • 42
  • 73
  • Sounds like a good idea. I haven't been able to test it yet, but when I get a chance I'll get back to you! – Jtaylorapps Jul 05 '12 at 13:27
  • Thank you! I am glad you find it useful. I am sure it can be improved, because I think it might break on subdomains. – starbeamrainbowlabs Jul 05 '12 at 13:33
  • Ok, well, I think this will at least work better than what I have currently. It actually doesn't work super well at this point, and I'll try to fix it, but it's currently returning this every time : `http://www` – Jtaylorapps Jul 06 '12 at 01:16
  • There. That should fix that, but it does not work very well for subdomains at the moment..... – starbeamrainbowlabs Jul 06 '12 at 06:57
  • Very good for the purpose here. I actually ended up getting the accurate name with document.title – Jtaylorapps Jul 06 '12 at 15:50
1

I found the solution!

For Xcode:

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

For Titanium:

theTitle = webView.evalJS("document.title");

Jtaylorapps
  • 5,680
  • 8
  • 40
  • 56