Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to send some url path to external js file


Code :
C#
// viewer.js    (file)
var DEFAULT_URL = 'sampletext.pdf' ;    // here i want to set pdf path which came from aspx.cs page 
var DEFAULT_SCALE_DELTA = 1.1;
var MIN_SCALE = 0.25;
var MAX_SCALE = 10.0;
var VIEW_HISTORY_MEMORY = 20;
var SCALE_SELECT_CONTAINER_PADDING = 8;
var SCALE_SELECT_PADDING = 22;
var PAGE_NUMBER_LOADING_INDICATOR = 'visiblePageIsLoading';
var DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT = 5000;


ple help me to solve it


thank you.....
in advance
Posted
Updated 9-Oct-18 3:52am
v2

Any javascript variables your code generates are usable from external js files, you just have to make sure the variable isn't read before it is defined. You can do this by making sure the js file is included after your variable is defined, or using something like the document ready event to run your code that accesses the variables. In this example I'm showing two ways of creating the variable, one is using a literal, the other using the script manager. If you are using razor then the first method will work with normal razor markup.

XML
<body>
    <form id="form1" runat="server">
    <div>
    <script type="text/javascript">
        var a = '<asp:Literal ID="LiteralA" runat="server" />';
    </script>
    </div>
    </form>
    <script src="External.js"></script>
</body>


C#
protected void Page_Load(object sender, EventArgs e)
{
    string text = "Hello";

    // Method 1, use a literal
    LiteralA.Text = text;

    // Method 2, get ClientScript to generate the markup
    string js = string.Format("var b = '{0}';", text);
    Page.ClientScript.RegisterStartupScript(this.GetType(), "setb", js, true);
}


The external.js file

C#
if (typeof (a) !== "undefined") {
    alert(a);
}

if (typeof (b) !== "undefined") {
    alert(b);
}
 
Share this answer
 
There are no "external" or "internal" JavaScript files. Here is how they work: you include a sequence of <script> … <script> elements in your HTML. Some include JavaScript inside the tag (instead of '…'), some reference files external to this HTML. And these fragments on JavaScript are processed in sequence as it was one bigger JavaScript file.

Therefore, the question makes no sense. But the code style looks really bad. You can do something like
JavaScript
var myData = {
   DEFAULT_URL: 'sampletext.pdf',  
   DEFAULT_SCALE_DELTA: 1.1,
   MIN_SCALE: 0.25,
   // ...
   DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT: 5000 }

// ...

var myResult = someFunction(myData);
// it will work even of function someFunction(data)
// is defined below, not in advance, 
// this is how JavaScript works

function someFunction(data) {
   var scale = data.DEFAULT_SCALE_DELTA;
   scale += // ...
   if (scale > data.MIN_SCALE)
      scale = data.MIN_SCALE; // just to illustrate the use of data 
   // ...
   return result;
}


In the code shown above, each declaration can be in a separate file, but you should not break declarations between files in an arbitrary place.

If you should have more than one object with the same properties as myData, you should use object constructor.

—SA
 
Share this answer
 
v2
Just to mention that the problem with placing script arguments "internally" in HTML files is that specifying an internal script will trigger CSP reports if the site is HTTPS and if CSP is fully enabled. The reason given in the CSP doc is that allowing internal elements can make injection easier.

This may sound technical, but someday the whole Web will only support HTTPS, making it a lot more secure with negligible performance impact and with free security certificates (such as are already available from Let's Encrypt and Comodo).

As an additional aside, a complete redesign of the Web is long overdue (including being able to recognize and disallow unauthorized code, which is likely to be malicious). One might hope that the many current languages used to specify web pages would be replaced by more comprehensive and easier to use languages, helping anyone to be able to design their own pages without having to learn a variety of different specification and programming languages.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900