Why is my external link rendered as internal link? - asp.net-mvc

I have a form that let people enter various links on my website like: http, https and ftp. When someone enters "http:stackoverflow.com", "https:stackoverflow.com" and "ftp:stackoverflow.com" and renders these in a view like this:
<ul>
#foreach (var link in Model.Keyword.References)
{
<li>
#link.Link
</li>
}
</ul>
I get the following output:
http:stackoverflow.com
https:stackoverflow.com
ftp:stackoverflow.com
This looks fine but when I hover "http:stackoverflow.com" the real link is: "localhost:1973/stackoverflow.com".
When I hover the https and ftp links I get the correct output:
https://stackoverflow.com
ftp://stackoverflow.com
I found out that the http links requires two "forward slashes" when I submit them via a form. So instead of "http:" you get "http://". I would like to know why https and ftp links do not need these extra slashes. I would also like to know a good solution for this because I don't think I should force an user to use these extra slashes.

The slashes in the URL are always required. It could be that your browser does some correction to the invalid url. Try to put https:stackoverflow.com in the addressbar of your browser and it will try to correct it in most browsers.

As for a good solution, I think you should only accept the domain without http://. You can put some small text underneath the input that shows "Example: 'www.somedomain.com'", so that the user knows the protocol in the front is not needed.
You can then use JQUERY/REGEX to validate and trim off any protocol stuff someone might add mistakingly.

Related

Create Link to External Website

I am trying to create an external link to each individual listing's assigned website address. Using the following code: (The listing website is saved as google.com)
External Link
Takes me to:
localhost:3000/google.com
Is there any way to generate a link that would go to www.google.com instead of trying to find a route in my application.
The reason why it's bringing you to localhost:3000/google.com it's probably because the string you are passing to the href attribute is not a full qualified URL.
In fact, if in HTML you write
External Link
The string will be appended to the current page path. You should make sure that the input you pass always contains the schema. If the input never contains that, then you can assume it's http://
External Link
But this is not really a solution, just a workaround. You should definitely make sure that when you populate the website URL, you store a complete URL. In fact, some sites may require https.
In Rails you normally use the url_for and link_to helpers to generate an URL, but they will both cause the same issue unless you pass a full URL.
<%= link_to "External Link", "http://#{listing.website}" %>
Do it the Rails way:
<%= link_to 'External Link', "http://#{listing.website}" %>
You need to put in the protocol.
Google
Do you get it? =)
You can create link like this:
External Link

How to use native anchor links with angularjs

I'm using angularjs on a rather large flat documentation page. The page has some navigation thats designed to use traditional url hash links. The urls look like so:
/documentation/flat#26166276-basic-events
These urls get rewritten once the navigation occurs and i've hit the next page. angular initializes to something like:
/documentation/flat#/26166276-basic-events
This breaks the navigation. It seems to work if I am already on the /documentation/flat path and hit one of the hash urls. It gets rewritten but the browser still focus's on the correct section of the page.
However if the the hash url is triggered from a different path the browser will not focus on the correct DOM element as the angularjs rewrite happens.
Edit: this is what the markup for a link looks like
Basic Events
<h1 class="chap-header" id="26166276-basic-events">2.1.0 Basic Events</h1>
This topic was further discussed here:
How to handle anchor hash linking in AngularJS
I used a variation from that thread
if $location.$$url[0]== '#'
$location.hash($location.$$url.replace('#', ''))
$anchorScroll()
that basically lets me prefix any anchor links with an additional # and angularjs treats them as traditional anchor
There is a very silly solution: put a / at the start of the anchor id!
<a id='/my-id' />

URL redirection JSF

I have my my app url: 127.0.0.1:8080/reader/read.xhtml
The read.xhtml is populated thorough database and has got various
links which are hard coded in database. (read.xhtml is actually
retrieved as String from DB.) for e.g. there are links
(<a href ="/write.xhtml>write</a>)
/write.xhtml
/upload.xhtml
as I cannot add the context when i click the link it directs me to
WWW://127.0.0.1:8080/write.xhtml or
HTTP://127.0.0.1:8080/upload.xhtml
Is there any way I can redirect the link to
HTTP://127.0.0.1:8080/reader/write.xhtml.
Can Prettyfaces handle this. If yes how?
You can simply add the context path to the links by rendering it in front of your links. Something like:
write

Anchor links don't work on pages with query strings

I've become dumbfounded by this. This might be something that I've just assumed worked all along, but in fact has never worked.
I've got an anchor link on a page [Activities] and later on the page I have the anchor <a name="activities"></a>. This is the URL of the page: https://iassid.org/index.php?option=com_content&id=216
For some reason, the anchor link on the page brings the user back to https://iassid.org/index.php#activities
Has removing the query string always been normal behavior? The href in the anchor tag doesn't include anything but the hash, why would it even assume to go off the page? Why does it go back to the original URL without the query string? Is there any way to get this to work without putting the entire URL including the query string in the URL as well? I'm trying to make this easy for someone who isn't very familiar with HTML, so using onclick events and other options aren't desired.
Maybe I've just been crazy to assume this would work all along! Thanks for any insights.

A shorthand for multiple links?

I have a fairly long list of quick links (approx. 20) that I'm going to embed on my main page. Instead of adding the full url of each link to the each anchor, is there a way to add maybe the end of each link.
For instance, if the link to the url is
http://support.proboards.com/index.cgi?display&thread=423890
as you can see, my forum is a proboards forum if that helps anything here.
the location of each url is local,
Instead of adding the complete url, is there a way to use something like
<a href = "&thread=423890">
or maybe
<a href = "this.thread=423890"> ?
Not in pure HTML, the best you'll get is <a href="index.cgi?display&thread=423890">, which is what you should already be using. Of course I'm assuming support.proboards.com is your site here.
You could use JavaScript and call a function like openThread(423890) if you really wanted, but a)I wonder what you really gain from it, b)this won't work for people with JavaScript turned off (a la NoScript), and c)I wonder if Google would properly see all the links.
Presumably, these pages are all being generated by server-side script anyway, so you should only really be coding index.cgi?display&thread= once anyway...
If you're hard-set on doing something shorter, I'd suggest using URL rewriting. You can have a URL like .../display/thread/423890 and then use something shorter if you're already from a /display/thread/ page. But this requires the link to go to a page you have control over.

Resources