I get a variable from the controller that is a java script code and want to init a variable of java script file with this data as a string.
What I mean is:
Model.TrialScript is equal to the string of:
<script type="text/javascript">var j = new Date();</script>
(I got it from the DB)
and then, I want to do the next thing in my js file:
var TrialScript = '<%= Model.TrialScript %>';
The problem is that TrialScript is not like I expect:
var TrialScript = '<script type="text/javascript">var j = new Date();
assuming I must get the js code as: <script type="text/javascript"> and not:
<script type=\"text/javascript\">, how can I solve this issue?
Another thing may helps: I want to run this script only when the user press a button (is called: button1)
Maybe is there an option to call this script from the js after the button is clicked without saving this script as a variable?
any help appreciated!
You can try that way:
var TrialScript = '<%= Model.TrialScript %>';
//remove script tags, get only the code
TrialScript = TrialScript.replace(new RegExp("^<script.*?>(.*)</script>"), "$1");
// transform the code as a JS function
TrialScript = new Function(TrialScript);
// associate to the button1
document.getElementById("button1").onclick = TrialScript;
Related
I would like to define a variable, which will include my filename only without any extension.
The example of my file is linked to the index.html page and it looks like this:
<script src="js/20231014.js" type="text/javascript"></script>
<script src="js/20211204.js" type="text/javascript"></script>
<script src="js/20230423.js" type="text/javascript"></script>
about 50 more files like these
so I want in my variable only the "20231014" extracted.
I found some nice solutions here:
How to get the file name from a full path using JavaScript?
from where I tried:
var filename = fullPath.replace(/^.*[\\\/]/, '')
but I got an error, that fullPath is not defined. That has led me to this link,
where I tried:
var fullPath = FileSystemEntry.fullPath;
but unfortunately, the error just changed, that fileSystemEntry is not defined.
My another approach was:
var fileurl = '/js/'.replace(/\.[^.]+$/, '')
where I got nothing
and finally
var fileurl = window.location.pathname;
where I got just index.html instead
Is there any swift solution for extracting the given filename from the link based in the other directory?
This code will do what you're after.
First we find the position of the last / character appearing in the
file-path.
Then we get the string that appears after the last / in the path.
Next we replace the '.js' part with nothing.
I've only performed a single check. That avoids printing an empty string for the script element that does all the work, since it's in the document and doen's have a source.
You'll need to add error-checking before this code is any good.
<head>
<script src="js/20231014.js" type="text/javascript"></script>
<script src="js/20211204.js" type="text/javascript"></script>
<script src="js/20230423.js" type="text/javascript"></script>
<script>
"use strict";
window.addEventListener('load', onLoad, false);
function onLoad(evt) {
let scripts = document.querySelectorAll('script');
scripts.forEach(scr => {
if (scr.src != '') {
let slashPos = scr.src.lastIndexOf('/');
let filename = scr.src.slice(slashPos + 1);
filename = filename.replace('.js', '');
console.log(filename);
}
});
}
</script>
</head>
I am trying to use a JSP variable in a .js file, but it doesn't populate anything in the variable. However, if I do the same in the jsp file inside block, it works fine. In other words:
Scenario#1
test.jsp (keeping everything in a jsp file) - works fine!!!
final String[] CONTENT = Processor.getContent();
final String projects= CONTENT[0];
<script>
function createPDD($container) {
var $projects = JSON.parse('<%=projects%>'); // this works fine and show the list of projects.
}
</script>
Scenario#2 test.jsp (keeping the js code .js file) - doesn't work!!!
<script src="test.js"> </script>
final String[] CONTENT = Processor.getContent();
final String projects= CONTENT[0];
test.js
function createPDD($container) {
var $projects = JSON.parse('<%=projects%>'); // this doesn't work... $projects comes empty.
}
Please note, its not a js import path issue as other js functions in test.js work fine.
Can anyone please guide me to read the value of Session timeout value using javascript contained in a .js file.
I need to access the value in a javascript file and process the value.
I tried to follow the below approach in a test.js file but it is not working:
var sessionTimeout = <%= Session.Timeout %>;
alert(sessionTimeout);
Your problem is that .JS files are not executed so they do not contain the Session.Timeout variable.
You can do two things:
Include your javascript directly in your ASP/ASPX page that does have the code being executed.
Register your JS script in your code.
Registering your JS
See: http://msdn.microsoft.com/en-us/library/aa479011.aspx#aspnet-usingjavascript_topic07
Page.RegisterClientScriptBlock("MyScript", _
"<script language=javascript src='MyJavaScriptFile.js'>")
I have share m code which I use for connection string from this code you can replace logic for session.
<script language="javascript" type ="text/javascript">
function ReadWebConfig()
{
var strCon = '<%=ConfigurationManager.ConnectionStrings["MysqlCon"].ConnectionString %>'
alert(strCon);
var strTemp = '<%=ConfigurationManager.AppSettings["WordTemplate"].ToString() %>'
alert(strTemp);
}
</script>
I am trying to use an external javascript provided by indeed.com but I can't get it to reload when I change the ind_q variable or the ind_l. It WILL work in IE9 and Safari but will not work in Chrome or Firefox unless I put them in incognito mode. I have tried the suggestion of appending a random # to the end of the ex. script with "?" but no luck. Any clues as to why it would work in 2 browsers but not the others? Guessing it is some kind of cache issue but don't know how to fix it. Also, if I change the ind_n variable (which is the # of results returned, it does change in all browsers)
These are the only variables I can change:
<script type='text/javascript'>
var ind_pub = '55555555555';
var ind_el = 'indJobContent';
var ind_pf = '';
var ind_q = 'job title';
var ind_l = '<?php echo $zip_code; ?>';
var ind_chnl = 'none';
var ind_n = 10;
var ind_d = 'http://www.indeed.com';
var ind_t = 20;
var ind_c = 10;
</script>
This is how I link to the Indeed script:
<script type='text/javascript' src='http://www.indeed.com/ads/jobroll-widget-v2.js'> </script>
If you want to force a new download of the script, it's a hacky workaround, but try loading the script dynamically (via a script) and appending something to the end of the URL. e.g. ...js?a=UNIQUE_NUMBER or ...js#UNIQUE_NUMBER. It's a hacky workaround, but could help
code :
<script type="text/javascript" src="http://127.0.0.1/Test.js#username=stackoverflow">
</script>
iwant to know ,how to get the username in Test.js
file
Test.js :
var username = ??
///////////// #username=stackoverflow
thanks advance
If you are trying to do all this on the client side, it's much better to use:
<script type="text/javascript">//<![CDATA[
var username = "stackoverflow";
//]]></script>
<script type="text/javascript" src="http://127.0.0.1/Test.js"></script>
That way, you don't need to tackle the issue of reading the src attribute of the script tag somehow.
The query portion of the URL is invalid. It should be:
http://127.0.0.1/Test.js?username=stackoverflow
The # is treated as a named anchor.
The gup function isn't good because the parameter is on the script tag, not the HTML output page.
The location object (location.href, location.search...) refers to the HTML page where the script included.
There are 2 other options:
Use this
Use #idealmachine answer. You can wrap the global variable with simple object in order to avoid conflict with other global JS variables