I'm trying to open a pdf that is located in the parent folder. I have no problem opening a pdf in the current directory or a child directory. However can't load the parent folder.
aLink.click(function(){
var pdf = "/../test.pdf";
window.open(pdf);
});
after running it we get Cannot GET /test.pdf.
Seems like it doesn't recognize /../ (accessing parent directory) Any help?
Write it this way:
var pdf = "../test.pdf";
Without the first slash
Many servers these days disable the ability to use relative links to access parent elements by default due to certain exploits where sloppy code can be used to expose your account level login information. Instead, you should use a base url so you can make your calls top-down.
var base_url = window.location.origin;
aLink.click(function(){
var pdf = base_url+"/folder/test.pdf";
window.open(pdf);
});
Related
I have created a page for a web banner under http://example.com/banner, I'm sending this link to publisher websites and pay them to run it.
However, some publishers run, some are not and I'd like to find which parent URL'S called for this page or where did the click come from. Generally, they are putting this URL in an iframe to serve it.
(Many pages doesn't pass referral parameter.)
I've tried different approaches with JS and PHP but as you might guess I'm getting http://example.com/banner as the parent URL.
Is there a way to know the parent URL from a different domain with PHP, JS or any other piece of code? I have a list of publishers but I also need to know which websites running the banner except for those sites.
To make it more clear here is a schema:
MY PAGE WITH BANNER > MY PUBLISHER WEBSITE > USER VISITING THE
PUBLISHER
I don't want to get IP of the user visiting my publisher's website or my page's
URL. I want to see URL of my publisher's website which is in between.
Since this is my web server I can read access logs, error logs etc. without issues.
I'm open to any suggestions.
Thanks!
You could try this, host a javascript file on your server.
Then they would place the script anywhere they want to put the banner:
<script src="//yoursite.com/banner.js"></script>
You could use params in that URL to then serve custom js.
Then fundamentally the code would look something like the following which injects the banner into the DOM where ever the script is placed. You get the sites URL from window.location.href and then send it as a param when requesting the image. (You could also use cookies etc)
<script>
// inject an anchoring element
document.write('<div class="banner_ad"></div>');
// find it
var parentDiv = document.getElementsByClassName("banner_ad");
// create the img/banner, notice the site param
var banner = document.createElement("img");
banner.src = 'http://via.placeholder.com/350x150?site=' + encodeURI(window.location.href);
// loop over the parent elements of each anchoring element
for (var i = 0, len = parentDiv.length; i < len; i++) {
// create the link
var link = document.createElement('a');
link.appendChild(banner);
link.setAttribute('title', 'Ads by Foobar');
link.setAttribute('href', 'http://example.com');
// inject the link and img
parentDiv[i].parentNode.appendChild(link);
}
</script>
Then server-side, look for the $_GET['site'] param.
It's not foolproof, nothing is.
Using Joomla to create my site. Let say my site is located at
https://domain.com/directory/siteName
I want to get this path in javascript on any page rendered on my site. The issue is, following internal sub menus and site's different settings, the path comes to be any one like these-
https://domain.com/directory/siteName/anyMenu/index.php
https://domain.com/directory/siteName/index.php
https://domain.com/directory/siteName/index.php?option=com_content&id=9
https://domain.com/directory/siteName/anyMenu
https://domain.com/directory/siteName/anyMenu.html
I have tried some solutions like-
var re = new RegExp(/^.*\//);
alert(re.exec(window.location.href));
It truncates the last node as index.php in case 1, also
var dummy = new Image;
dummy.src = '$';
alert(dummy.src.slice(0,-1));
does the same thing. So is there any way to get exact address where site is located using javascript only? Also the result must be generic for any site as if it is applied in any extension then that extension can be installed in any site with any level of directory structure.
I require this as I am using a js file which is internally registering another js file. So path for this, to be registered js has to be given in first js
You can use Joomla's JURI::base() function for that. Remember it's php. so you could do something like:
$document = JFactory::getDocument();
$document->addScriptDeclaration('var base = \''.JURI::base().'\'');
You can now use the base variable in your JS script. Remember to add it to the document after the declaration above.
I created a html page (stored locally) that uses Googlemaps API. My whole page is a basic google map with some customization. I want to take a screenshot every time I change some parameters in the customization so later I can easily compare.
I've found this similar answer and I got it to work on my machine. However, when I change the url from an actual webpage into my own local html file, Phantomjs only saves an entirely black image.
Here is my script.
var WebPage = require('webpage');
page = WebPage.create();
page.open('googlemaps_demo.html');
page.onLoadFinished = function() {
page.render('myScreenShot' + '.png');
phantom.exit();}
The file googlemaps_demo.html and this js script itself are in the same folder. Could someone explain to me why this code only works for an online url, but not a local html file? And how to fix it?
You probably need to specify file using a file:/// scheme and a full location of your file, e.g. file:///c:/local/page.htm
I have a .NET MVC5 application using C#, HTML and Javascript.
I need to know the link of the host so I can send a specific file in a specific folder to the user.
In my local computer, when I test and develop the application, the path the app uses is the following:
localhost:1234/Home/Scripts/myScript.js
However, in the real deployment server, the path changes:
www.superhost.com/Apps/MyApp/Home/Scripts/myScript.js
I am trying to send this file to the user with the following JQuery, when a button is clicked:
$("a.btn.btn-default").click(function () {
download("/Scripts/myScript.js"); //download is a personal function, unimportant
return false; //prevent browser defualt behavior
});
The problem is that when I click the button, I get in the consolo an erorr - 404 error, which means the server is not finding the file.
In fact the server is searching for the file on the path "/Scripts/myScript.js", but the file is in "Apps/MyApp/Home/Scripts/myScript.js".
How do I make my javascript smart enough to figure the correct path?
One solution I use a lot is to inject a site-root URL into the page using something like this:
<body data-root="#Url.Content("~/")">
Which converts to the actual website base URL at runtime.
You then use that injected value, from all jQuery code, using:
var root = $('body').data("root");
You can simply prepend that to any relative URLs to make them work correctly:
$("a.btn.btn-default").click(function () {
download(root + "Scripts/myScript.js"); //download is a personal function, unimportant
return false; //prevent browser defualt behavior
});
Note: this approach will work in cases where the routing changes:
e.g / vs /home/index/ vs /home/ which are all the same page, but different URLs
Remove the leading / from your download path.
You actually don't need an absolute path (and to know server's home folder) to access a file, target it relatively from the page you invoke the download from.
note: If you are using page relative paths, you must make sure you don't move the page the download script is executed from.
I always use in my layout the next code:
<script type="text/javascript">
var rootUrl = "#Url.Content("~/")";
</script>
And when I need it, i use for example:
$("a.btn.btn-default").click(function () {
download(rootUrl + "Scripts/myScript.js"); //download is a personal function, unimportant
return false; //prevent browser defualt behavior
});
I have a spring based web application MyWebapp built using maven and deployed on Websphere 6.1
The folder structure is:
MyApp --> src ---> main --->
The main folder is further having resources and webapp folders.
webapp folders is having other folders like images, theme, jscript, JSP, META-INF, WEB-INF
images folder is having icons folder with say example.png
So fetching example.png on localhost as:
http://localhost:9080/MyWebapp/images/icons/example.png
succeeds.
In jscript folder I have a sample.js javascript file where some functions are defined.
I am importing this javascript file in JSP pages as:
<script src="<%=request.getContextPath()%>/jscript/sample.js" type="text/javascript" language="Javascript"></script>
This javascript file is having a function which tries to fetch image as below:
iconFile = '../images/icons/search_result.png';
anchor.style.backgroundImage = 'url(' + iconFile + ')';
anchor.style.backgroundRepeat = 'no-repeat';
anchor.style.backgroundPosition = '1px 2px';
anchor.className = 'toplevel-tab';
The complete function basically tries to place a icon before some text in JSP.
The code gets parsed. However, the image does not get displayed.
Running the code independently on a simple html with the png images in the same folder as html and javascript files succeeds. Here i will just have iconFile = "search_result.png"
So, it is not code issue.
Issue is that the image is not getting located or the server is unable to find the image in above javascript code.
What am I doing wrong ?
How can I solve it ?
The answer for https://stackoverflow.com/a/8298652/887235 which I accepted earlier does not work.
So please do not downvote this question as a duplicate one.
Also I am working on restricted environment where access to programs like Tail will not work.
Changing
iconFile = '../images/icons/search_result.png';
to
iconFile = '/images/icons/search_result.png';
also does not work!!
Thanks for reading!
You just have to understand how relative paths work. Even if the path is in a JavaScript file, the path is not relative to the location of this JS file, but it's relative to the URL of the HTML page being displayed in the browser.
So, if the URL of the page executing this javascript code is
http://foo.bar.com/myWebApp/zim/boom/tchak.html
and the URL of the image is
../images/icons/search_result.png
The absolute URL of the image will be
http://foo.bar.com/myWebApp/zim/boom/../images/icons/search_result.png
which is the same as
http://foo.bar.com/myWebApp/zim/images/icons/search_result.png
An absolute path like /images/icons/search_result.png is also resolved from the root of the web server, and not the root of the webapp (the browser doesn't know what a Java webapp is and doesn't care). So it's resolved as
http://foo.bar.com/images/icons/search_result.png
You would need to prepend the context path to the path to make it really absolute:
<%=request.getContextPath()%>/images/icons/search_result.png
or, without scriptlets:
${pageContext.request.contextPath}/images/icons/search_result.png
You need to give your javascript an awareness of the path to the root of your application, as this will change on context. Start by declaring a global variable, such as:
<script>
var siteroot = "<%=request.getContextPath()%>";
</script>
Then, you are ready to use it later, such as:
iconFile = siteroot + '/images/icons/search_result.png';