GetGlobalContext is not defined in HTML webresrouce of Dynamics CRM - javascript

I've an HTML web resource in Dynamics CRM on-premise environment. It is saved as new_htmlpage1, now at a click of Custom button I'm opening this HTML web resource. I've added ClientGlobalContext.js.aspx as reference in HTML webresource as well. But still I am receiving error that GetGlobalContext is not defined
Below is my HTML source code.
<html>
<head>
<script src="../ClientGlobalContext.js.aspx" type="text/javascript"></script>
<script type="text/javascript">
function tempContext(){
if (typeof GetGlobalContext != "undefined") {
var userName = Xrm.Page.context.getUserName();
alert(userName);
return;
}
}
</script>
</head>
<body onload="tempContext()">
</body>
</html>
Here is the walkthrough from Microsoft that I am following, but still unable to get GlobalContext.
Please let me know what I am missing here.

The path to ClientGlobalContext.js.aspx is a relative path (relative to the location of your web resource), so ensure that you navigate up the necessary amount of times. E.g. one of the following might be what you need to use, depending on your web resource:
<script src="ClientGlobalContext.js.aspx" type="text/javascript" ></script>
<script src="../ClientGlobalContext.js.aspx" type="text/javascript" ></script>
<script src="../../ClientGlobalContext.js.aspx" type="text/javascript" ></script>
You should then call the function GetGlobalContext() from JavaScript to get access to the global context.
Right now you are trying to call Xrm.*, which is not available.
As the documentation for the GetGlobalContext function states:
Including a reference to ClientGlobalContext.js.aspx does not make the
Xrm object available in HTML web resources. Therefore, scripts
containing Xrm.* methods aren’t supported in HTML web resources.
parent.Xrm.* will work if the HTML web resource is loaded in a form
container. However, for other places, such as loading an HTML web
resource as part of the SiteMap, parent.Xrm.* also won’t work.

Related

html script blank page when run

I'm trying to debug an experiment written in JS & html. I'm running an html template that imports JS codes. But all I get is a blank page when I run it in chrome. I checked whether my chrome preference allows javascript to be run.
<!doctype html>
<html>
<head>
<title>Name Face Association Task</title>
<script defer src="/Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/static/lib/jquery-min.js" type="/Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/text/javascript"></script>
<script defer src="/Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/static/lib/underscore-min.js" type="/Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/text/javascript"></script>
<script defer src="/Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/static/lib/backbone-min.js" type="/Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/text/javascript"></script>
<script defer src="/Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/static/lib/d3.v3.min.js"></script>
<!-- jsPsych Plugins -->
<script defer src="/Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/static/jsPsych/jspsych.js"></script>
<script defer src ="/Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/static/jsPsych/plugins/jspsych-instructions.js"></script>
<script defer src="/Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/static/jsPsych/plugins/jspsych-html-keyboard-response.js"></script>
<script defer src="/Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/static/jsPsych/plugins/jspsych-survey-html-form.js"></script>
<link href="/Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/static/jsPsych/css/jspsych.css" rel="stylesheet" type="/Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/text/css"></link>
<!-- additional functions -->
<script defer src="/Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/static/js/supplementary-functions.js"></script>
<script defer src="/Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/static/js/questionnaire.js"></script>
<!-- psiTurk
<script defer src="/Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/static/js/psiturk.js" type="text/javascript"></script>
<script type="text/javascript">
// These fields provided by the psiTurk Server
var uniqueId = "{{ uniqueId }}"; // a unique string identifying the worker/task
var adServerLoc = "{{ adServerLoc }}"; // the location of your ad (so you can send user back at end of experiment)
var mode = "{{ mode }}"
</script>
-->
<!-- experiment -->
<script defer src="/Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/static/js/NFA.js"></script>
</head>
<body>
<noscript>
<h1>Warning: Javascript seems to be disabled</h1>
<p>This website requires that Javascript be enabled on your browser.</p>
<p>Instructions for enabling Javascript in your browser can be found
here</p>
</noscript>
</body>
</html>
My psiturk is not able to be downloaded, so I commented out all the code related to psiturk and tried running it in plain html instead.
I guess your server does not have a path like http://example.com/Users/jasonlim/.... :)
You should open your browser's devtools (F12) to see the errors. It would have told you that those files cannot be found.
You need to make those paths relative to the current page (something like static/lib/jquery-min.js or lib/jquery-min.js depending on where your HTML file is located), or when running a local HTTP server you can make them relative to the root of the domain (e.g. /lib/jquery-min.js).
Plus, you have invalid type attributes. The MIME type for JavaScript is text/javascript and not /Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/text/javascript. Same goes for the style tag, the MIME type should be text/css and not /Users/jasonlim/Downloads/MTurk_Experiments-master/NFA/text/css.
You can check the documentation about <script> and <style> here. Also, take a look at this tutorial to understand relative and absolute URLs.
But, it appears to me that you are looking at this the wrong way in the first place. According to the psiTurk documentation, you need to use the psiturk tool (for example with the debug command) to test your experiment locally, not by opening the HTML file manually. Your comment says "These values are provided by the psiTurk server" but if you just open the HTML files, there is no such psiTurk server, and you will have variables actually containing {{ uniqueId }} for example, instead of the ID that the server would normally put there...

[react,ES6]I fail to access object in http script label in index.html , from my own JS page

This is the script in my index.html
<script type="text/javascript" src="http://api.map.baidu.com/api v=2.0&ak=eBGR7XzaPhB5UbYARl3E7ksdkMdgrCw7"></script>
and I try to access a object --"BMap" from the script in my JS page which look like this:
var map = new BMap.Map("allmap"); // 创建Map实例
then I get error:"error! BMap is not defined"
what should I do?
I am guessing that you wanted to use BMap that is provided by baidu api. I changed the src that is actually pointing to the script and it seems to work. Note that you probably want to change the https to http when you are developing locally, as it may not work otherwise. See working example below (I had to use https because stackoverflow uses it by default).
If you have any other js module that uses BMap, make sure that it is called after the baidu api script is declared. S
<html>
<head>
<script type="text/javascript" src="https://api.map.baidu.com/getscript?v=1.1&ak=&services=true&t=20130716024058"></script>
<script type="text/javascript" src="../path/test.js"></script>
<script type="text/javascript" src="../otherpath/someOtherjs.js"></script>
</head>
<body>
<script>
// You can use BMap here...
console.log(BMap);
</script>
</body>
</html>

Libraries not loading on form load- CRM 2016

We are upgrading from CRM 2011 to CRM 2016 on premise.
I have all the libraries added on the form load in address entity form but those libraries aren't being loaded at the first time ans showing error message 'Mscrm' is undefined in the below code snippet and page.
<script language="JavaScript">
window.setTimeout(ribbonRefreshForArticle, 2000);
function ribbonRefreshForArticle() {
var uri = Mscrm.CrmUri.create(window.location.href);
if (uri.get_query() != null && uri.get_query()["etc"] == Mscrm.InternalUtilities.EntityTypeCode.KbArticle) {
refreshRibbon();
}
}
</script>
Page- http://myOrg/testDev/userdefined/edit.aspx?_CreateFromId={447AE3EE-2727-E511-84E4-005056B33BEB}&_CreateFromType=2&_gridType=1071&etc=1071&id={858C47D7-62AE-43DE-A13B-2F648EFD111E}&pagemode=iframe&rskey={03315B35-4585-4447-A4D2-059CF79CA0FD}
Need help.Thanks in advance.
I'm a little confused by your snippet, You're showing the JS within the context of an HTML script tag, but you're defining it as being called from the Form Load.
If you have an html web resource, you must first add the ClientGlobalContext <script src="../ClientGlobalContext.js.aspx?type=script" type="text/javascript"></script> ("../" may vary depending on your resource path), and then wait for your document to be ready:
$(document).ready(() => {
onLoad();
});
You have to declare your libraries in the file that contains your javascript
like:
<head>
<script src="cs_jsQuery" type="text/javascript"></script>
<script src="ClientGlobalContext.js.aspx" type="text/javascript"></script>
<script src="cs_CimailXrmProximaIncident" type="text/javascript"></script>
</head>

Web Worker Javascript cannot find functions in other javascript files

I have an HTML file like this:
<html>
<head>
<title>World Aviation</title>
<script language="javascript" src="./jquery-min.js"> </script>
<script language="javascript" src="./jsDraw2D.js"> </script>
<script language="javascript" src="./script.js"> </script>
</head>
<body>
<input onClick="startAnimBackground();" type="button" value="Start"/>
<script language="javascript">
initAirports();
initRoutes();
var w;
function startAnimBackground()
{
if(typeof(Worker) !== "undefined")
{
if(typeof(w) == "undefined")
{
w = new Worker("start_script.js");
}
} else {
startAnimation();
}
}
</script>
</body>
</html>
the file "start_script.js" contains only 1 line which calls the startAnimation() function. This function is located in "script.js"
My problem is: my browser supports web-workers, so it creates a web-worker and loads the file "start_script.js", but it fails to call the function "startAnimation()" indicating that it cannot find the function. But this function is located in script.js which is included in the head section. So, the only conclusion I can draw is that web-worker JS files have a different scope and thus is not able to include the 3 javascript files specified in the head section. So what should I do to make this work?
Thanks.
Web workers don't share functions, variables, or scripts with the main script.
To load dependencies, call importScripts() in your worker.
To allow your web worker to interact with or affect the main script or the DOM, you have to use message passing and have the main script listen for, interpret, and act on the messages.
See: https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers

Uncaught ReferenceError when accessing an object from another JS file

I have various JS libraries in my web application, which are loaded before my main JS file (main.js). One of these libraries is jshashtable, but when I try to create a new Hashtable object in main.js, Google Chrome and Firefox throw a ReferenceError, complaining that the variable does not exist.
Here is the <head> of the application:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="/static/jquery-1.4.4.min.js"></script>
<script type="text/javacsript" src="/static/jshashtable-2.1.js"></script>
<script type="text/javascript" src="/static/main.js"></script>
Here is the problem line in main.js:
posts = new Hashtable();
This line is inside a function called init which is called when the page has finished loading (using the jquery $(document).ready() function).
Any reason why Hashtable is not global? Google maps and jquery objects work with no such problem. The source of jshashtable can be seen on Google code.
Updated answer: The problem is that you've got a typo in the script tag:
<script type="text/javacsript" src="/static/jshashtable-2.1.js"></script>
<!-- ^^---- here (the letters are transposed) -->
I couldn't understand why you would be running into a problem and decided to actually copy-and-paste your script tags and replicate the structure exactly on my machine. And things stopped working and my world tilted 3° counter-clockwise until I finally stared at them long enough to see it.
Provided that the jshashtable code really is at /static/jshashtable-2.1.js and your server is serving it up correctly (double-check on Chrome's resources tab in the dev tools), I can't see any reason for that. Your scripts are in the right order, and jshashtable's docs show using a global Hashtable (and the code link you gave clearly shows it creating one).
Edit: I've just replicated that same structure (same scripts, same order, using jQuery(document).ready(function() { ... });) on my own server, and am not having that problem. I can create a Hashtable and use its functions.
My HTML:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Test Page</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type='text/javascript' src='jquery-1.4.4.js'></script>
<script type='text/javascript' src='jshashtable-2.1.js'></script>
<script type='text/javascript' src='main.js'></script>
</head>
<body>
</body>
</html>
My main.js:
jQuery(document).ready(function() {
try {
var ht = new Hashtable();
display("typeof ht = " + typeof ht);
display("ht.size() = " + ht.size());
}
catch (e) {
display("Exception: " + e);
}
function display(msg)
{
$("<p>").html(msg).appendTo(document.body);
}
});
Only difference is I'm not using a /static prefix, and I'm absolutely certain that makes no difference.

Categories