I use chrome.extension.getURL for a file, and on the page it is placed, it treats it as a relative url (e.g. http://example.com/chrome-extension://ajs8dh8dsfauhdf8auhaffh/blah.js)
How can I make it treat it as an absolute URL instead? It is placed into the href component of a tag.
EDIT: I've seen people's plugins do this for CSS, so I know it is possible. Maybe not for href attributes?
chrome.extension.getURL should return a URL that starts with chrome-extension://. For example if you did chrome.extension.getURL("blah.js"); the value returned would be similar to "chrome-extension://ajs8dh8dsfauhdf8auhaffh/blah.js". This URL points to a local file stored in the extension's directory that is created when the extension is installed. The "ajs8dh8dsfauhdf8auhaffh" is a hash representing your extension to chrome. You're obviously getting something close to that, but the question is why is your's prefixed with "http://example.com/". I would check how you are setting the href attribute.
Related
In my Reactjs application I used the anchor tag to download a txt file like below.
<a href="http://textfiles.com/......./sample.txt" download>download</a>
There I added download attribute to force it to download instead open it in the browser tab.
But it is still opening in the same tab instead of downloading. Can any one help me to solve this problem.
Looking at your example seems you're not using same origin and that might have cause the problem.
If you're trying to download the file which exists in same origin then I suggest you to use relative url rather than absolute url.
Example:
<a href="/public/sample.txt" download>download</a>
Please take a look at the note from docs:
Attribute: download
Notes:
This attribute only works for same-origin URLs.
Although HTTP(s) URLs need to be in the same-origin, blob: URLs and
data: URLs are allowed so that content generated by JavaScript, such
as pictures created in an image-editor Web app, can be downloaded.
If the HTTP header Content-Disposition: gives a different filename
than this attribute, the HTTP header takes priority over this
attribute.
If Content-Disposition: is set to inline, Firefox prioritizes
Content-Disposition, like the filename case, while Chrome prioritizes
the download attribute.
Try like this
import File from 'http://textfiles.com/......./sample.txt'; //Or the path could be any relative path to the local file.
//Other code
render() {
return(
//Code
<a href={File} download>download</a>
)
}
But inorder to use like this, you should be confident that you will get the file without any issues. Better to download the file and access it from your own directory instead of calling from another server.
I am checking the source of a webpage whose url is
http://localhost:12345/web/query?name=time&
In the source of the html webpage, I saw a relative url:
Date
Is the relative url in the source supposed to be appended to the url of the webpage? In the above example, is it
http://localhost:12345/web/query?name=time&./query?name=time¶ms=3
Or is how to interpret the relative url in the webpage completely up to the author of the webpage?
When a web browser renders the webpage, the web browser interprets the relative url as
http://localhost:12345/web/query?name=time¶ms=3
I wonder how the browser can guess how the author interprets it.
Thanks.
./ is relative to the directory the file is in. I'd never use paths like that personally.
If the file is /web/query then Date and Date would essentially be the same thing. Obviously that link has to be found at the first level inside the /web directory or it wouldn't point to the right place, which is why I never use those type of paths, I always use paths that start at the document root, /.
These types of paths are usually found when developers either can't or don't know how to set up a proper web server for the site in my experience.
I am developing a website on a web server which can be accessed by 2 URL: mywebsite.example.com or example.com/mywebsite. For example, when I access mywebsite.example.com/images/abc.jpg and example.com/mywebsite/images/abc.jpg, I get the same picture.
The problem is, I have many links inside my website, and I am not sure should I use an absolute or relative path.
From another question
Absolute vs relative URLs
I found someone suggesting using URL relative to root (like /images/abc.jpg), however when I access the website using example.com/mywebsite, every link just break.
For relative paths, I found it hard to manage since webpages are in different folders, but using the same template which contains some links. It means I have to manually set some links as ../ and some as ./.
I have also tried using <base> tag however it messes up with anchor. Even if I try to include the full path before the # symbol, some jQuery libraries does not function properly since they get the value inside the attribute href directly, but not extracting the part after #.
Would there be any better practice or suggestion?
I think you should use relative urls, and concentrate your searchs on how to use relative urls in templates, that would be resolved relatively to the final page.
I don't know the technology you are using for templating, but I see two common solutions :
declare a "relative path" variable in the template, and then override it in the different pages, with the new relative path. Use this relative path as a prefix for all urls
delegate urls construction to a service that would know the final page. Somethinkg like resolveUrl(..)
How does an anchor tag work that does not specify a filename?
Example:
Fast Cars
I was under the impression the HTML file had to be expressly cited, but in this case it is not, just the directory name.
Does this reference an HTML file, or is it something else?
Depending on the webserver configuration, pointing to a directory might mean pointing to the index file of that folder, or even at whatever other file you want to rewrite.
There was a time in which SEO practices reccomended using folder style permalinks instead of query strings. So there were cases in which
/index.php?section=fastcars
was rewritten as
/fastcars
Again, this is all rewrite magic. Most webservers offer some kind of rewrite rules to achieve this.
Besides, an a element doesn't need to point to a file or url address. They can be used as internal navigation links to point to an anchor, and JS frameworks such as jQuery have popularized its use only as button replacements to trigger a given behavior.
An anchor tag's href attribute does not reference a file; it references a URL. What that URI corresponds to is completely opaque. In the most common case where the URL uses the HTTP(S) protocol, that URL is sent to the server as part of a request.
It's the server, and only the server, that determines what the URL corresponds to. It doesn't have to be a file or anything else that you might consider tangible, and you can't tell looking at it from the client side.
The main function of the "a" attribute is to create links. That means a link to an external website:
<a href="https://www.stackoverflow.com">
or a link within the same page. Just create an id - for example:
<h2 id="top">text text text</h2>
then create a link using that id -
Top
Every time you click on the link, your browser will send you to the part of the page where your id is.
Suppose I had a URL in my browser location bar that read:
http://www.example.com/us/books
... how do I code the url in the page so that it'll add to the address in the location bar?
Example
Url in location bar: http://www.example.com/us/books
Url on page: Read more
Desired
http://www.example.com/us/books/this-title
No matter what I do, It wont appear after the entire URL. The URL isn't fixed.
This approach of using a relative path inside a reference does work. But you have to figure out some details which depend on your local situation.
In your case most likely the target url called is http://www.example.com/us/this-title (you did not tell us...). This is due to the fact how the browser (not the server!) interprets its current position inside the document hierarchy on the server.
If your base url reads http://www.example.com/us/books/ (note the trailing "/") then things would work as expected by you! You could even note the relative path as Read more. This works because the browser recognizes the current position as a folder and assumes the relative path to be located inside. This works for example when the html page holding that reference is delivered by an index document (index.html, index.php or similar) on the server. This is when the trailing slash makes sense, since it denotes that the current url retrieved the content of a folder, not a directoy.
Of course this is only what is happening on the client side. It might be that the server decides to rewrite the url and redirect the browser again. For example by using rewrite rules inside the http server. This is typically done to handle requests to non existing documents.