I have used this tutorial to create a JS based widget. One thing I need to do is to pass query string parameters in JS file. I tried document.location.href but it gave the URL of page where widget was placed(which is quite obvious)
Code is given below:
<script src="http://example.com/widget.js?id=2" type="text/javascript"></script>
<div id="widget"></div>
I need to fetch id=2 which I can pass further.
Thanks
If you give your script an id then you can write:
<script src="http://example.com/widget.js?id=2" id="myscript" type="text/javascript"></script>
<div id="widget"></div>
<script>
var myScript = document.getElementById('myscript');
var src= myScript.getAttribute('src');
//Get the id from the src based on parameter using Regular Expression
</script>
Related
I've got a jotform that I want to prepopulate fields.
This code works, but I want to dynamically update the name after the "=".
<script type="text/javascript"
src="https://form.jotform.com/jsform/202786957650165?fullName3[first]=Tony">
</script>
let's assume I can grab the name from the url, such as https://example.com?Tony using the following
var fname = window.location.search.substr(1)
my question is how do I get the name added to the JS?
This doesn't work:
<script type="text/javascript"
src="https://form.jotform.com/jsform/202786957650165?fullName3[first]="+fname>
</script>
any ideas?
I wanted to access a session variable in javascript in asp.net mvc application. I have found a way to do it in aspx view engine but not in razor.
Please tell me a way to access the session variables
You can do it this way for a String variable:
<script type="text/javascript">
var someSessionVariable = '#Session["SomeSessionVariable"]';
</script>
Or like this if it's numeric:
<script type="text/javascript">
var someSessionVariable = #Session["SomeSessionVariable"];
</script>
This is really not a very clean approach though, and requires inline JavaScript rather than using script files. Be careful not to get carried away with this.
I personally like the data attribute pattern.
In your Razor code:
<div id="myDiv" data-value="#Request.RequestContext.HttpContext.Session["someKey"]"></div>
In your javascript:
var value = $("#myDiv").data('value');
In my asp.net I am not getting the result by
<script type="text/javascript">
var someSessionVariable = '#Session["SomeSessionVariable"]';
</script>
But I get the answer by below code,
<script type="text/javascript">
var yourVariable = '<%= Session["SessionKey"] %>';
</script>
For google searchers,
In addition, If you want to access the session variable in external .js file you can simply do like this,
------ SOME HTML PAGE ------
//Scripts below Html page
<script>
//Variable you want to access
var mySessionVariable = '#Session["mySessionVariable"]';
</script>
// Load External Javascript file
<script src="~/scripts/MyScripts/NewFile.js"></script>
Inside NewFile.js
$(document).ready(function () {
alert(mySessionVariable);
});
I know this question are asked hundreds of times :( but I just want to learn more :).
My question is simple, can I pass a value to a js file like this, if not, how ?
<script type="text/javascript" src="./js/create.js?method=create"></script>
Yes, you notice that I have a parameter method=create which I want to use in my create.js.
I know in jquery ajax, we have an easy way, but you must notice that the ajax method is in included in the js file, how could I pass a parameter to the js file itself ?
Any answer is welcome :)
Thanks.
This works perfect for me:
<script src="js/script.js" id="myScript" data-url="my_variable"></script>
inside the script.js file:
var myUrl = document.getElementById("myScript").getAttribute( "data-url" );
"myUrl = my_variable" inside the code
you can use myUrl everywhere.
Not really, no. What you can do is this:
<script type="text/javascript">
var method = "create";
</script>
<script type="text/javascript" src="./js/create.js"></script>
Another way is to only define functions inside your javascript file, and then invoke after it has loaded.
<script type="text/javascript" src="./js/create.js"></script>
<script type="text/javascript">
runMyLoadedCode("create");
</script>
Third way is belying my first simplistic answer: access the script tag itself and parse it. You can see here how to access the tag that has loaded your script; take its src value and cut it up to locate your method=create.
I am trying to grab information from the page url to set the src for a data file.
So, say page url is: page.html?x=data_file_3
(The ideas is I could change the url to access other data files: data_file_4, etc.)
I grab the "data_file_3" part of the url and put it in a variable:
(the code I use for this works fine -- so result is)
folder = "/data_file_3/content.js" -- the content of this file is just an array
Then I try this:
<script id="url" type="text/javascript"></script>
<script language="javascript">
...
var u = document.getElementById('url');
u.src = folder;
...
</script>
But this doesn't work (the array data does not show up on the page). I put this code right where I used to hard code:
<script type="text/javascript" src="/data_file_3/content.js"></script>
The hard-coded version works. Any ideas about how I can do this?
Sounds like you are trying to create script tags dynamically.
var scr = document.createElement('script');
scr.src = 'script_path';
document.getElementsByTagName('head')[0].appendChild(scr);
You can wrap this in a function where 'script_path' is whatever you're path you're passing in.
Note also that 'text/javascript' is not required. All browsers understand that its javascript.
Is there any way that in an external javascript file, can know the host of the file?
For example, if I have the site http://hostOne.com/index.php, the code of the file index.php:
<html>
<head>
<script type="text/javascript" src="http://hostTwo.com/script/test.js"></script>
</head>
<body>
<div>...</div>
</body>
</html>
I need that in the file test.js can know the host http://hostTwo.com.
Thank you.
EDIT
or it can know the tag "script" which was called?, with this option I can analyzes the tag and get the "src" attribute. But I don't want to depend on the name of the file test.js and analyze all the tag script that contains the site.
*Solution based on the code of #Armi *
Html:
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script id="idscript" type="text/javascript" src="http://hostTwo.com/script/test.js"></script>
</head>
<body>
<div>...</div>
</body>
</html>
code in JS
var
url = $('head').find('#idscript').attr('src'),
host = url.replace(/(\/\/.*?\/).*/g, '$1');
console.log(host);
I've got an idea (the snippet based on jQuery):
var yourScriptTag = $('head').find('script[src$="jquery-1.7.1.js"]').eq(0);
var theHostnameOfYourScript = $(yourScriptTag).attr('src').replace(/(http:\/\/.*?\/).*/g, '$1');
alert(theHostnameOfYourScript);
jsfiddle example: http://alpha.jsfiddle.net/XsJn8/
If you know the filename of your script (and if this is always the same and unique) you can use this snippet to get the hostname.
If this path is relative (and contains no host) you can get the hostname with a simple location.hostname
Sorry, not possible. The content of the script is downloaded and after this it is fired. At this point the script "thinks" he is at your site.
Of course unless the host is hardcoded in the script.
This is not possible, because the JavaScript code is executed client-sided. You could propably parse it somehow out of your URL but, I don't think either that this is very useful and possible.
Inside test.js, you can use :
var url = document.URL;
then parse the url result.
You can't make cross-site scripting, so if you need more sophisticated stuff, you could write your javascript in php and call :
<script type="text/javascript" src="http://hostTwo.com/script/test.php"></script>
But that's not standard.
Anyway,, the solution is on the server, with a designed proxy.