I am working on an ASP.NET Web Forms project that make use of Routing.
I have two routing situations:
http://mywebsite.com/section
http://mywebsite.com/section/subsection
My js files are stored in a /js folder in the root of the project.
Fact is, when I load /section page everything works fine, the js are loaded correctly. When I load /section/subsection page the js are not loaded and actually the path appear to be something like
http://www.mywebsite.com/section/file.js
I have already tried to put a slash before the name of the js folder or even to use a server side ResolveUrl function like
<script src='<%=ResolveUrl("~/js/file.js")%>'></script>
In this case file.js il loaded correctly; anyway if file.js contains calls to an external js file, the latter is not loaded (its path is always in the form of /section/externalfile.js).
Adding the following line to the Global.asax
routes.Ignore("{*allfiles}", new { allfiles = #".*\.(css|js|gif|jpg|png)" });
did not solve the problem either.
The most surprising thing is the fact that, for instance, image or CSS files are loaded correctly even with a normal link like the following:
<link rel="stylesheet" type="text/css" href="css/owl.theme.css" media="all" />
Is there anyone who has an idea of how solving this issue ?
Edit: The master page of my project calls a cutom.js file (which is loaded correctly). This file contains for instance the following call to superfish.min.js:
Modernizr.load([{
load: kopa_variable.url.template_directory_uri + 'js/superfish.min.js',
complete: function() {
$('.top-menu').superfish({});
}
}]);
Related
I have an HTML page that has a reference to some scripts like these ones:
<script charset="utf-8" src="Content/Styles/ThemeForest/assets/js/vendors.js"></script>
<script charset="utf-8" src="Content/Styles/ThemeForest/assets/js/app.js"></script>
The problem is, that when I upload my site to a server, it is no longer working.
I figured that if I instead add the web application name, it will work again. Something like this:
<script charset="utf-8" src="MySite/Content/Styles/ThemeForest/assets/js/vendors.js"></script>
<script charset="utf-8" src="MySite/CContent/Styles/ThemeForest/assets/js/app.js"></script>
Now, the thing is, that I may not know how the web application name would be like once it is uploaded to a server... so, I would like to dynamically add the application name to the src.
I noticed that there is a JavaScript function called window.location.pathname, which can bring me the web application name (if any). I could concatenate this to the rest of the route and it should work.
Now, the catch is, this is all on a regular HTML page.
Is there a way that I can set a custom route to those script tags using basic JavaScript functions? Or any other ideas?
You can use something like $script, just add that lines of code in your app.run or any other place, and call it with an array of the routes of the js files that you want to load. You said that you could get the web application name, so just pass the name of the app concatenated with the path of the script:
var dependenciesLoadingFactory = function(deps) {
return ['$q', '$rootScope', function($q, $rootScope) {
var deferred = $q.defer();
$script(deps, function() {
$rootScope.$apply(function() { deferred.resolve(); });
});
return deferred.promise;
}];
};
If you link to a source document starting with a directory or the filename itself (like you did in your question), the lookup will look for it starting in the same folder that your current file (this would be the html file that holds your script tags) is in.
You could also link to files in parent directories relative to your current location, like this:
../file.js
You can alternatively link to a path starting with a slash:
/dir/to/your/file.js
This will start looking in the root directory of your page, meaning the directory your domain or subdomain points to. You can't successfully link to files that are above your root directory.
Finally, if your files are on another server, make sure you include the full path including the http:// snippet:
http://example.website.com/dir/file.js
Under normal circumstances, there should really be no reason to deviate from these methods, but if you absolutely have to dynamically set a directory, I suggest you use php because it's server-side and therefore faster for the user, and available if they have javascript disabled (which is important if you link to other files such as style sheets in this way):
<?php $directory = '/dir/'; ?>
<script src="<?php print $directory; ?>file.js"></script>
Of course for this to work, your server has to have php running.
I am getting problem in referencing Javascript file.
My all js files reside in my js folder:MyProject/Js/*.js
Now on my master page i am refering my file like this:
<script src="js/jquery-1.9.1.js"></script>
<script src="js/jquery-1.9.0.min.js" type="text/javascript"></script>
Error showing in console:
SyntaxError: expected expression, got '<'.
On searching i have found that this issue comes due to inappropriate referencing of js files so i have resolve this error by doing this:
<script src="../js/jquery-1.9.1.js"></script>
<script src="../js/jquery-1.9.0.min.js" type="text/javascript"></script>
This above solution works when my .aspx page would be like this:
MyProject/Admin/Abc.aspx //This will work
But problem will occur when any of my .aspx page would be like such:
MyProject/Admin/Subfolder/Abc.aspx // This will not work
My Abc.aspx page contains master page but now in this case this will not load my js files due to ../
So can anybody tell me whats the correct way to reference js files which will work in both the cases??
Note:I am referencing all js files in to my master page and my master page is in:
MyProject/MasterPage.Master
I think you can give a try:
1) Use Bundling to reduce the loading time (Your script will be shorter as well)
2) Use ~/ instead of ../ to make your script/code work even if you relocate the pages.
You can easily find the scripts and codes of jquery bundle if you create a new ASP.NET application in Visual Studio.
In my assets/www/index.html, I am trying to open html in the
/data/data/files/xyz/index.html with this javascript command:
window.location.href = cordova.file.dataDirectory + "xyz/index.html";
In the ../xyz/index.html it also included the cordova.js which exists on the same xyz directory:
<script type="text/javascript" src="cordova.js"></script>
The index.html can be loaded, but in the catlog show error like below
W/CordovaWebViewImpl﹕ Blocked (possibly sub-frame) navigation to
non-allowed URL: gap://ready
is there something wrong with my method? Is it wrong to use window.location.href to open another cordova application in the data folder ?
EDIT : I already found the root cause, that is my cordova.js in the /data/data/files/xyz/cordova.js is having different version with the one in asset folder. after i copied the same cordova js version, it can be loaded without error warning. Thanks.
You don't need to use "cordova.file.datadirectory". That's only if you use the file plugin for accessing data files, such as saving high-scores in a game or level data. You do not use the plugin for loading/unloading pages into the current webview.
You would want all of your HTML files to be in the same folder branch as your "index.html". Assume a directory structure like this:
/ index.html <!---- this is your current index.html
/ page2.html
/ js / index.js
/ xyz /index.html
All you would have to do is window.location='xyz/index.html'. However, I strongly encourage you to not replace entire pages if you're developing for iOS. It's almost a guarenteed way to get your app rejected. Try loading your pages in via AJAX using a framework like "Framework7".
NOTE: You can only view files within the webview, that are in the same folder or are children of, as your initial index.html.
EDIT: You want to load another Cordova webview application. I'd suggest still making it a child of the initial "index.html" folder. However, you might be able to write a plugin or customize the platform itself to access the other files. However, that's out of the scope of this question!
I am very confused.
I have been testing a site I am building on one server, and on that server I have a link to a javascript file. On said server I link to the file with '../js/javascript.js' and the file loads properly. On another server I just switched to, if I use the same link the file does not load. I look at firebug and see it seems that '../' is going up two directories and returning a 404 error.
I have fixed the path on the new server, but I am confused about the inconsistency.
I believe it has to do with the fact that I call the javascript in my header.php file, which is in a directory called inc. however the index.php where I include my header.php is in the root directory. But, I don't understand why on one server it references from the root and on the other it references from inside the inc directory.
site/
index.php
js/
javascript.js
inc/
header.php
It does not matter where header.php exists, because it gets included in index.php before the page is delivered to the browser. So any <script src="..."> tags will be relative to index.php. So, your HTML for including the js file should be:
<script src="js/javascript.js">
No ../! Not sure why it worked on the old server, but it definitely won't work on the new one as you've described it.
I am sure there is a very simple answer to this question, I think am just having trouble. I have animation.js file. It also has a include folder with dependency_1.js and dependency_2.js. In my animation.js file I do load dependency_1.js and dependency_2.js relative to my animation.js file.
In my _Layout.cshtml file I do have:
#RenderSection("JavaScript", required: false)
And I did place within my view SomeView.cshtml
#section JavaScript
{
<script type="text/javascript" src="#Url.Content("~/Scripts/animation.js")"</script> }
But when my animate.js file calls the dependency_1.js file I get a 404 Not Found - http://localhost:14611/includes/dependency_1.js"
Im guessing it has to do with the url routing to the Scripts folder in my MVC project?
Relative paths in Javascript files are relative to the HTML page, not the source Javascript.
You need to use an absolute path.