I am trying to add following code in GTM to measure Web Core Vitals
<script type="text/javascript">
new PerformanceObserver((entryList) => {
for (const entry of entrytList.getEntries()) {
const elm = entry.element;
console.log(elm);
}
}).observe({type: 'largest-contentful-paint', buffered:true});
</script>
This code works in Console but when i try to publish it in GTM it generates error message as below
Not sure what is wrong with the code as it should support ECMA16 for GTM
Yes, GTM is pretty slow when it comes to ES6 adoption. It partially adopted it for things like templates, but not fully even there. And obviously it didn't adopt it for custom html tags.
What you'll have to do is rewrite your code to be ES5 compliant. Or you can use babel to do an automated transition. it will look like this:
new PerformanceObserver(function (entryList) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = entrytList.getEntries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var entry = _step.value;
var elm = entry.element;
console.log(elm);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}).observe({ type: 'largest-contentful-paint', buffered: true });
You probably don't need all the try catch and finally, but babel is being honest with what it does.
I'm trying to use the Kaazing Library in our HTML5 client. I already implemented it in a java client and it worked. It seems that there is a problem with the LoginHandler. When I debug the code it ends in a endless loop in the LoginHandler. The line callback(new PasswordAuthentication(usr, pwd)) is called over and over again:
// Configure a Basic Challenge Handler
var basicHandler = new BasicChallengeHandler();
basicHandler.loginHandler = function(callback) {
callback(new PasswordAuthentication(usr, pwd));
}
JmsConnectionProperties jmsProps = new JmsConnectionProperties();
jmsProps.connectionTimeout = 1000000;
jmsProps.reconnectAttemptsMax = -1;
jmsProps.reconnectDelay = 3000;
jmsProps.shutdownDelay = 5000;
console.log("Connect to: " + url);
// Create Connection Factory
jmsConnectionFactory = new JmsConnectionFactory(url, jmsProps);
websocketFactory = jmsConnectionFactory.getWebSocketFactory();
websocketFactory.setChallengeHandler(basicHandler);
// reate Connection future handler for the result
try {
if (connection == null) {
var connectionFuture = jmsConnectionFactory.createConnection( function() {
try {
// never comes to this line!!!
connection = connectionFuture.getValue();
// DO SOME STUFF
} catch (e) {
console.dir(e);
// alert(e.message);
}
});
} else {
try {
connection.close(function() { /* Closed */
});
} finally {
connection = null;
}
}
} catch (ex) {
console.dir(ex);
}
Any help would be very appreciated!
Regards
Angela
Is it possible that the username and password combination is incorrect, so that the client is being re-challenged?
I am trying to associate an XMLHttpRequest with a tab on the browser using the following code:
function getBrowserFromChannel(aChannel) {
var notificationCallbacks =
aChannel.notificationCallbacks ?
aChannel.notificationCallbacks :
aChannel.loadGroup.notificationCallbacks;
if (!notificationCallbacks) {
console.log("no callbacks");
return (0);
}
var loadContext = notificationCallbacks.getInterface(Ci.nsILoadContext);
getInterface(Ci.nsILoadContext) fails with: "Component does not have requested interface"
Any idea how else I can get the browser?
Thanks
Try this code (from Lightbeam):
function getLoadContext(aRequest) {
try {
// first try the notification callbacks
var loadContext = aRequest.QueryInterface(Ci.nsIChannel)
.notificationCallbacks.getInterface(Ci.nsILoadContext);
return loadContext;
} catch (ex) {
// fail over to trying the load group
try {
if (!aRequest.loadGroup) return null;
var loadContext = aRequest.loadGroup.notificationCallbacks.getInterface(Ci.nsILoadContext);
return loadContext;
} catch (ex) {
return null;
}
}
}
Note the license is MPL 1.1/GPL 2.0/LGPL 2.1
The following code alerts ls exist in IE7:
if(window.localStorage) {
alert('ls exists');
} else {
alert('ls does not exist');
}
IE7 doesn't really support local storage but this still alerts it does. Perhaps this is because I am using IE9 in IE7 browser and document modes using the IE9 developer tool. Or maybe this is just the wrong way to test if LS is supported. What is the right way?
Also I don't want to use Modernizr since I am using only a few HTML5 features and loading a large script isn't worth it just to detect support for those few things.
You don't have to use modernizr, but you can use their method to detect if localStorage is supported
modernizr at github
test for localStorage
// In FF4, if disabled, window.localStorage should === null.
// Normally, we could not test that directly and need to do a
// `('localStorage' in window) && ` test first because otherwise Firefox will
// throw bugzil.la/365772 if cookies are disabled
// Also in iOS5 & Safari Private Browsing mode, attempting to use localStorage.setItem
// will throw the exception:
// QUOTA_EXCEEDED_ERRROR DOM Exception 22.
// Peculiarly, getItem and removeItem calls do not throw.
// Because we are forced to try/catch this, we'll go aggressive.
// Just FWIW: IE8 Compat mode supports these features completely:
// www.quirksmode.org/dom/html5.html
// But IE8 doesn't support either with local files
Modernizr.addTest('localstorage', function() {
var mod = 'modernizr';
try {
localStorage.setItem(mod, mod);
localStorage.removeItem(mod);
return true;
} catch(e) {
return false;
}
});
updated with current source code
if(typeof Storage !== "undefined")
{
// Yes! localStorage and sessionStorage support!
// Some code.....
}
else
{
// Sorry! No web storage support..
}
This function works fine:
function supports_html5_storage(){
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch(e) {
return false;
}
}
Source: www.diveintohtml5.info
Also I don't want to use Modernizr since I am using only a few HTML5
features and loading a large script isn't worth it just to detect
support for those few things.
To reduce Modernizr file size customize the file at http://modernizr.com/download/ to fit your needs. A localStorage-only version of Modernizr comes in at 1.55KB.
Try window.localStorage!==undefined:
if(window.localStorage!==undefined){
//Do something
}else{
alert('Your browser is outdated!');
}
You can also use typeof window.localStorage!=="undefined", but the statement above already does it
I didn't see it in the answers, but I think it's good to know that you can easily use vanilla JS or jQuery for such simple tests, and while Modernizr helps a lot, there are clean solutions without it.
If you use jQuery, you can do:
var _supportsLocalStorage = !!window.localStorage
&& $.isFunction(localStorage.getItem)
&& $.isFunction(localStorage.setItem)
&& $.isFunction(localStorage.removeItem);
Or, with pure Vanilla JavaScript:
var _supportsLocalStorage = !!window.localStorage
&& typeof localStorage.getItem === 'function'
&& typeof localStorage.setItem === 'function'
&& typeof localStorage.removeItem === 'function';
Then, you would simply do an IF to test the support:
if (_supportsLocalStorage) {
console.log('ls is supported');
alert('ls is supported');
}
So the whole idea is that whenever you need JavaScript features, you would first test the parent object and then the methods your code uses.
Try catch will do the job :
try{
localStorage.setItem("name",name.value);
localStorage.setItem("post",post.value);
}
catch(e){
alert(e.message);
}
Try:
if(typeof window.localStorage != 'undefined') {
}
if (window.localStorage){
alert('localStorage is supported');
window.localStorage.setItem("whatever", "string value");
}
Modifying Andrea's answer to add a getter makes it easier to use. With the below you simply say: if(ls)...
var ls = {
get: function () {
var test = 'test';
try {
localStorage.setItem(test, test);
localStorage.removeItem(test);
return true;
} catch(e) {
return false;
}
}
};
var ls = {
get: function () {
var test = 'test';
try {
localStorage.setItem(test, test);
localStorage.removeItem(test);
return true;
} catch(e) {
return false;
}
}
};
function script(){
if(ls){
alert('Yes');
} else {
alert('No');
}
}
<button onclick="script()">Local Storage Support?</button>
I know I'm a little late to the party, but I have a few useful functions I cooked up and threw into a file named 'manage_storage.js'. I hope they are as useful to you guys, as they have served me well.
Remember: The function you're looking for (that answers this question) is isLclStorageAllowed.
So without further ado here is my code:
/* Conditional Function checks a web browser for 'session storage' support. [BEGIN] */
if (typeof isSessStorageAllowed !== 'function')
{
function isSessStorageAllowed()
{
if (!!window.sessionStorage && typeof sessionStorage.getItem === 'function' && typeof sessionStorage.setItem === 'function' && typeof sessionStorage.removeItem === 'function')
{
try
{
var cur_dt = new Date();
var cur_tm = cur_dt.getTime();
var ss_test_itm_key = 'ss_test_itm_' + String(cur_tm);
var ss_test_val = 'ss_test_val_' + String(cur_tm);
sessionStorage.setItem(ss_test_itm_key, String(ss_test_val));
if (sessionStorage.getItem(ss_test_itm_key) == String(ss_test_val))
{
return true;
}
else
{
return false;
};
sessionStorage.removeItem(ss_test_itm_key);
}
catch (exception)
{
return false;
};
}
else
{
return false;
};
};
};
/* Conditional Function checks a web browser for 'session storage' support. [END] */
/* Conditional Function checks a web browser for 'local storage' support. [BEGIN] */
if (typeof isLclStorageAllowed !== 'function')
{
function isLclStorageAllowed()
{
if (!!window.localStorage && typeof localStorage.getItem === 'function' && typeof localStorage.setItem === 'function' && typeof localStorage.removeItem === 'function')
{
try
{
var cur_dt = new Date();
var cur_tm = cur_dt.getTime();
var ls_test_itm_key = 'ls_test_itm_' + String(cur_tm);
var ls_test_val = 'ls_test_val_' + String(cur_tm);
localStorage.setItem(ls_test_itm_key, String(ls_test_val));
if (localStorage.getItem(ls_test_itm_key) == String(ls_test_val))
{
return true;
}
else
{
return false;
};
localStorage.removeItem(ls_test_itm_key);
}
catch (exception)
{
return false;
};
}
else
{
return false;
};
};
};
/* Conditional Function checks a web browser for 'local storage' support. [END] */
/* Conditional Function checks a web browser for 'web storage' support. [BEGIN] */
/* Prerequisites: 'isSessStorageAllowed()', 'isLclStorageAllowed()' */
if (typeof isWebStorageAllowed !== 'function')
{
function isWebStorageAllowed()
{
if (isSessStorageAllowed() === true && isLclStorageAllowed() === true)
{
return true;
}
else
{
return false;
};
};
};
/* Conditional Function checks a web browser for 'web storage' support. [END] */
Here is how the code is.
function AppAssistant() {
this.dbObj = new DatabaseAssistant();
this.schemaObj = new SchemaAssistant();
this.result = {};
}
AppAssistant.prototype.setup = function() {
}
AppAssistant.prototype.handleLaunch = function(launchParams) {
if (gConfigDatabase.engine == "sqllite") {
} else {
}
}
AppAssistant.prototype.processResult = function() {
}
AppAssistant.prototype.execMigrations = function(version) {
}
// Database error handler
// If the db does not exists, it will generate set of tables
AppAssistant.prototype.errorHandler = function() {
Mojo.Log.info("In app asst error handler.");
if (gConfigDatabase.engine == "sqllite") {
try {
// execute some queries to create tables and insert some values in the HTML Opendatabase.
// Show disclaimer Page
Mojo.Controller.stageController.pushScene('disclaimer');
} catch (e) {
Mojo.Log.error("In AppAssistant errorHandler : ", e);
}
} else {
Mojo.Log.info("db8 part of app asst error handler");
try {
// execute operations to create kinds for db8 analogous to tables
Mojo.Controller.stageController.pushScene("disclaimer");
} catch (e) {
Mojo.Log.info("Error in db8 appAsst error handler", e);
}
}
}
AppAssistant.prototype.handleCommand = function(event) {
}
All this was done to support both sqllite and db8 for an webOS app. The problem i am facing is that when i configure a variable (gConfigDatabase) as sqllite then the Mojo.Controller.stageController.pushScene` method which is there in the errorHandler function works. but when i change it so that db8 engine is used, then i get an error saying undefined method pushScene. Any clues or hints on debugging or solving this ?