I used DOM in order to extract all HREF-s from given html source. But, there's a problem: If i have link like this one:
<LINK rel="alternate" TYPE="application/rss+xml" TITLE="ES: Glavni RSS feed" HREF="/rss.xml">
then "href" element will be presented as /rss.xml, although that "/rss.xml" is just anchor text. Clicking on that link from Chrome's page source view, real link is opened.
I would like to take that href-s LINK, not anchor text. Please, how can i do it with dom?
Get a hold of the link element and get its href property. Suppose you were using an id,
<link id="myLink" rel="alternate" href="/rss.xml" />
var link = document.getElementById("myLink");
link.href; // http://www.example.com/rss.xml
"href" element will be presented as /rss.xml
Yes, that is the value of the attribute
although that "/rss.xml" is just anchor text.
No. <link> elements don't have anchor text. In the following example 'bar' is anchor text.
bar
Clicking on that link from Chrome's page source view, real link is opened.
Browsers know how to resolve relative URIs.
I would like to take that href-s LINK, not anchor text. Please, how can i do it with dom?
You can't use DOM to resolve a URI. You use DOM to get the value of the attribute and then use something else to resolve it as a relative URI.
The article Using and interpreting relative URLs explains how they work, and there are tools that can help resolve them.
You need to know the base URI that the relative URI is relative to (normally the URI of the document containing the link, but things like the base element can throw that off)
In Perl you might:
#!/usr/bin/perl
use strict;
use warnings;
use URI;
my $str = '/rss.xml';
my $base_uri = 'http://example.com/page/with/link/to/rss.xml';
print URI->new_abs( $str, $base_uri );
Which gives:
http://example.com/rss.xml
You can try using document.location.href to get the current URL and append the result you are getting from your example. That should give you an absolute path for the link.
Related
I have an href link set to output to target=‘right’. ‘right’ is an I frame in the parent window.
<a href=‘http://www.example.com/abc.pup?ev=$ev’ target=‘right’>Click</a>
The desired target, ‘right’ is in the parent document. The link is not finding the target. It worked well until I I did some editing.
The target attribute specifies where to open the linked document:
You can see details from this link:
https://www.w3schools.com/tags/att_a_target.asp
The right syntax would be:
Click
SOLVED. When the required link href is written in full as http://www.example.com/rqdpage.php the output will be a new window regardless of the target stated, probably for security reasons. The required target is implemented when the required page is in the same directory as the calling page and is stated as a relative path. Thus
<a href=‘rqdpage.php?ev=$ev’ target=right></a>
worked perfectly. Output in frame “right”. Thanks all.
This seems like a very basic HTML question, but I cannot find an answer here or elsewhere that actually works.
What I want to do is jump to an id link on the same document without reloading the document.
Here's my setup. The document is http://www.example.com/mydocument.htm/.
<head>
.
<base href="http://www.example.com">
.
.
</head>
<body>
<!-- Jump from ... -->
<div>
Jump to here.
</div>
<!-- Jump to ... -->
<div id="myid">
<Do stuff>
<Do more stuff>
</div>
</body>
This syntax, according to everything I have read on this site and elsewhere, is supposed to result in a jump within the current document without a page reload.
Doesn't work. My browsers (Firefox, Chrome) automatically stick the base href in front of the bookmark, viz: http://www.example.com/#myid, which opens my home page.
Not what I want.
If I change the href from "#myid" to /mydocument.htm#myid, then the jump completes, but the page reloads. Ditto if I use the absolute address: http://www.example.com/mydocument.htm/#myid.
I'm stuck. Any guidance?
The <base> element instructs the browser to append the URL in the href to all relative URLs on the page. So having:
<base href="http://www.example.com" />
Means that for :
here.
The href is handled as :
http://www.example.com/#myid
Instead of
<current_page>/#myid
You almost certainly don't need that <base> element in the head section, especially based on your further point that using the full URL (which also has http://www.example.com in it) works, meaning your page is already at http://www.example.com and thus doesn't need to make it explicit with <base>.
Alternatively (and I don't actually recommend this, because your use of base seems incorrect), you could change the href of your link to be the current page plus the id hash, like:
here.
As the browser will render the URL (when applying the base href) to :
http://www.example.com/mydocument.htm/#myid
and thus not try to leave the current page as it will treat it the same as if the base weren't set. (Note that this would only work when you have the base href set to the URL of the actual page's base, and as I mentioned earlier, that would make the base element unnecessary).
https://jsfiddle.net/ouLmvd3g/
If you are considering a javascript solution, since the <base> is apparently never necessary, I would recommend an event listener that removes the base element from the DOM rather than your suggested :
a fix using an event listener to remove the base URL for local links
A simple solution would be:
window.onload=function(){
var baseElement = document.getElementsByTagName("base")[0];
baseElement.parentNode.removeChild(baseElement);
}
https://jsfiddle.net/vLa0zgmc/
You could even add a bit of logic to check if the base element's href matches the current page's actual URL base, and only remove when it does. Something like:
var baseElements = document.getElementsByTagName("base");
if (baseElements.length > 0) {
var baseElement = baseElements[0];
var current_url = window.location.toString();
var base_url = baseElement.getAttribute("href");
// If the base url and current url overlap, remove base:
if (current_url.indexOf(base_url) === 0) {
baseElement.parentNode.removeChild(baseElement);
}
}
Example here : https://jsfiddle.net/gLeper25/2/
Thanks to all who responded.
In the end it turns out I was asking the wrong questions. What I needed was a means of jumping to an anchor on the same document without the document reloading. Unfortunately I got fixated on the problem with <base> interfering with the normal <a href....> process.
The actual answer was to use onClick instead, and the code was provided by #Davide Bubz in "Make anchor links refer to the current page when using <base>", and it's simple and elegant, using document.location.hash instead of <a href...>:
Anchor
where "test" is the ID identifying the item to be jumped to.
Several responders pointed to this thread as answering my issues, but I was not smart enough to understand its import until I had read it for the third time. Had I been smarter, I would have saved 6-1/2 hours of wasting my time on trying to fix the <base> problem.
Anyway, problem solved. Thanks to all and especially to Mr. Bubz.
Im on the page:
example.com/news
It's a list of news articles. Is it possible to set links on each news article and take into account the current url?
link to something
The above will link to:
example.com/news/article
Or will I need to get the entire route and specify that in the link?
The url could be anything eg. /products so I do not want to hardcode it in.
If you need to take into account the current path, use the page name directly in the href attribute:
If you are on example.com/news and used an href value of "article", the URL becomes example.com/news/article.
If you need to reference pages on the root directory, precede the page name with slash '/', href="/article".
Make it relative?
link to something
For some browsers/DOCTYPE, you may have to use this in conjunction with the base tag element, which will need to be added to every page that utilises relative paths:
<base href="http://www.example.com/news">
<a href="#"
onclick="
window.open(
'https://www.facebook.com/sharer/sharer.php?u='+encodeURIComponent(location.href),
'facebook-share-dialog',
'width=626,height=436');
return false;">
Share on Facebook
</a>
Apologies if this is a beginner question. But why is "href" there? What purpose does it serve? I am not getting its purpose.
I am assuming "onclick" a new window will be opened which has all the information in it about the link to be shared and the FB target destination.
Then why is "href" there????
HTML anchors (<a> tags) define clickable links, but only if the href attribute is present. (It is possible to not have the href element, but either a name or id instead; in this case it is not a clickable link, but rather a destination for a link). See the HTML specification for more detail.
The # element on a href attribute indicates a predefined anchor, for instance, http://domain.com/page#section. In this case, when the link is clicked the browser will open the page at http://domain.com/page, and in its HTML the browser expects a section called #section, defined by a <a name="section"> on its code. Upon finding this section, the browser will position the screen at this position.
When the section isn't specified, just the # is provided, it means it's a link pointing to the local page (or more specifically, the top of it). If the page isn't at the top, it jumps to the top. If it is at the top, nothing happens.
You can test it by creatin an HTML file with a text link, it will be clickable, but there will be no effect (the only effect is, if it isn't at the top, it will jump to the top).
The return false; at the end of the JavaScript code is there to prevent the "jump to the top", if you remove it, the browser will position its view at the top of the page upon clicking.
Some will argue that href should always point to actual links and using # placeholders is bad practice.
It specifies where the URL goes to. In this case it's set to # meaning it won't open up a URL but instead will do nothing. It's telling the 'a tag' that this function will DO something.
See it as a default measure.
Hope that helps :)
Tags <a> are not considered as clickable links if they does not contains a href.
My problem is i wanna have some comment or disable some text inside value of href attribute of anchor tag.
I mean look like this:
Jquery.com
And when i click url redirect to stackoverflow.com and behave with domain.com like a comment or don't have any effect to link. Value in square bracket just like a comment inside url :[domain.com]
Can i do this without using jquery or javascript just like // or /**/ for normal comment inside code.
Please help me and thanks for reading.
Comments in HTML are this way :
<!-- [domain.com] -->
but that won't work in your case.
Why do you need to leave the [domain.com] if you want it to be effectless ?
Anyway, you could do something like that :
Jquery.com
and JS:
$('a.linkWithComment').click(function(e) {
$(this).attr('href', $(this).attr('href').replace(/\[(.*)\]/g,''));
});
Here's a JSFiddle for it : http://jsfiddle.net/kJDUB/
BUT be aware that this is absolutely not a recommended behaviour: SEO is probably broken, and there a many solutions to achieve same behaviour while being cleaner (like HTML5 tags : Jquery.com
and you can still reach the domain.com ...)