ASP.NET 2.0 provides the ClientScript.RegisterClientScriptBlock() method for registering JavaScript in an ASP.NET Page.
The issue I'm having is passing the script when it's located in another directory. Specifically, the following syntax does not work:
ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptName", "../dir/subdir/scriptName.js", true);
Instead of dropping the code into the page like this page says it should, it instead displays ../dir/subdir/script.js , my question is this:
Has anyone dealt with this before, and found a way to drop in the javascript in a separate file? Am I going about this the wrong way?
What you're after is:
ClientScript.RegisterClientScriptInclude(this.GetType(), "scriptName", "../dir/subdir/scriptName.js")
use: ClientScript.RegisterClientScriptInclude(key, url);
Your script value has to be a full script, so put in the following for your script value.
<script type='text/javascript' src='yourpathhere'></script>
Related
I have the following javascript/jquery snippet in a View of my vb.net based asp.net mvc project. I am trying to hit up the Browse method of my Neutrals controller.
<script type="text/javascript">
window.location.href = '/Neutrals/Browse?AreaOfLaw=' +
$('#search_area').val() + '&WebRegionID=' + $('#search_region').val();
</script>
I'd like to replace the url string with an #Url.Action, but I can't figure out how to fix the Razor syntax and that of the query parameters that I have.
I've fiddled with it for a good while and most of the time I can't even get it to compile.
Unfortunately, I don't think you can do it.
The problem is that you need those JavaScript variables - i.e. #Url.Action is executed when page is rendered on the server and you don't have JavaScript variables then.
Sure, you can construct part of url on the server, but the querystring part will need to be in JavaScript.
Use is directly as
window.location.href = '#Url.Action("action","controller")'
I put a function in an existing.js file (I tried two different files) so that the method would be available multiple places without repeating the code. I even created a simple function just to make sure it wasn't something my function was messing up.
function doNothing() {
alert("Dammit.");
}
I can see the method in the intellisense list in my .ascx control, but as soon as I try to step into it, it fails.
Below is how I tried to reference the file and it seemed to work as far as seeing the methods, but it won't work.
<script src="/javascript/messages.js" language="javascript" type="text/javascript"></script>
Any ideas? Any common mistakes that I may be making? It looks like it should work.
Note: when the methods are inside the .ascx file, they work fine, even with missing semi-colons.
Even if IntelliSense suggested "/javascript/messages.js", the path will be wrong if your application is installed under a virtual directory instead of at the root of the Web site. For example, the full URL of the JavaScript file might be at http://www.example.com/myapp/javascript/messages.js, but because the src attribute starts with a /, the browser will access http://www.example.com/javascript/messages.js (without the "myapp" virtual directory) instead.
If you're using WebForms, you can change the src attribute as follows:
src='<%= this.Page.ResolveClientUrl("~/javascript/messages.js") %>'
I would like to edit part of the .js file. I mean add some code, and these code should be executed when I open the website (in browser). How I can do that?
The easiest way to do that is to use jquery and put your code into
$(document).ready(function() {
//your code
}
or if you don't want to use jQuery
document.onload = function()
{
//your code here
}
If you want to add on to existing JavaScript code for a site, I am not sure that you can in a permanent way. However, if you want to change some sort of behaviour just for you, you could take a look into creating a userscript. There are many examples at userscripts.org.
You cannot edit the js file but you can override the functions and the variables. This will work unless the js file is not protected by closure.
You can't modify the js file on client side. However, if you just want to add some code, you can use userscript.
this is a bad...
but if the js is not remote, use AJAX to read the file contents, then change what you need then use the DOM to create the script tag and use .innerHTML to put the content in.
if you would like code tell me if you want JQuery or native and i will post
P.S if the file is remote there is no way to do it...
I need to dynamically add a script reference, so I do this:
jQuery('html head').append("<script src='somesource.com/somejs.js'><\/script>")
and it does't work - I don't get any errors but I can't execute any of the methods defined inside that script.
Any ideas what I am doing wrong?
jQuery has a getScript method:
$(document).ready(function() {
$.getScript('somesource.com/somejs.js');
});
Without seeing the script in context, it is hard to say, but possibilities include:
You have the URL wrong (you have what appears to be a domain name, but no protocol in the URI)
You are trying to use the functions without allowing time for the browser to download and run the script (so they aren't defined at the time you call them)
You need a type='text/javascript':
jQuery('html head').append("<script type='text/javascript' src='somesource.com/somejs.js'><\/script>")
I want to run the following jquery code on every page in my website.
$(document).ready(function(){
$("#more").click(function(){
$("#morediv").slideToggle("slow");
return false;
});
});
In all my pages I have the more and morediv elements defined, for every page I have different js file and adding this code in every file will not be a good solution (I suppose).
I have created a global.js to include this code, but in other pages also I have the $(document).ready(function(){} function defined and may be that's why its conflicting and not running properly.
You can have multiple $(document).ready(function(){}) elements on your page, so that it's the problem. I suggest using Firefox/Firebug and examining any console errors you find to discover the problem. Perhaps your global.js file is being loaded before jQuery itself? Otherwise, you'll need to dig into it with Firebug's debugger.
Are you actually doing some server-side programming or you are talking about plain HTML pages. I would advise that you have templates (this is specific to your development environment and tools of choice) and include the JS in those templates. Then the actual pages will all use the template and have the JS available. The question you are asking has in fact nothing to do with Javascript or JQuery, but the way you organize your site... unless I'm missing something.
having $(document).ready() event handler in global.js and the page it is included in does not poses any problem I'm using it and it works really fine.
Just a guess, but are you referencing the location of the global.js file correctly?
To be sure, write something like the following into your global script:
$(document).ready(function(){
alert("document ready");
$("#more").click(function(){
$("#morediv").slideToggle("slow");
return false;
});
});
If you don't get the alert the script is not pathed correctly, or is not placed after the jquery include (or the jquery include is not pathed properly).