Thymleaf javascript variable combining - javascript

I'm using thymeleaf in my spring boot project. It's working well. Now I need to render one url in JavaScript as a string and need to concatenate with one JavaScript variable. I have tried the following code.
location.href = /*[[#{/signage/save}]]*/ '' + res.id
But the generated output is
location.href='/signage/save';
What I want is following
location.href = '/signage/save' + res.id;
How can I achieve it?

You can tell Thymeleaf to uncomment certain code if the page is served dynamically using special comment syntax /*[+...+]*/. And inside this commented block, you can put expressions and they will be evaluated together with the whole block.
/*[+ location.href = [[#{/signage/save}]] + res.id +]*/
Will be rendered as
location.href = '/signage/save' + res.id

After trying few methods got the solution, not exactly what I needed but it works for me. I just wrapped it using parenthesis ((.....))
location.href = (/*[[#{/signage/save}]]*/ '') + res.id
and generated output is
location.href = ('/signage/save') + res.id;

Related

Javascript - Forward Slash appended automatically in URL with the Window.Location.Hash

In my Angular JS website, I wish to append the text "#Page/" to the URL using Javascript. Here my problem is that the forward slash(/) is getting added when using the code Location.hash
For example, Let say the URL is https://MyWebsite.com/Products
var location = window.location;
location.hash = "#page/" + pageNumber;
window.location = location
After executing the above code, the Window.location turned out to be https://MyWebsite.com/Products/#page/1.
But what I need is the forward slash(/) should not be included between products and #page.
https://MyWebsite.com/Products#page/1.
The strange thing is that when I checked the same code with the other websites don't have Angular JS, implemented only with the Asp.net Webforms and ASP.net MVC, I got the desired result.
Does this kind of issue have something to do with the Angular JS?
I believe # is special character reserved for a page fragment reference.
See https://www.ietf.org/rfc/rfc3986.txt
Can you do with without # or use the # to reference the fragment? e.g.
window.location.href += 'page/' + pageNumber + '#' + pageFragment;

Require file in Cypress using string templates

I'm trying to require a js file in cypress and it works with a static string but as soon as I try string templates, it no longer finds the file.
const page = require('../../cypress/model/page/web/app/Homepage.js');
//const page = require(`../../cypress/model/page/web/app/${pageName}.js`);
As # pavelsaman already commented, you should first check what is in pagename. simply a console.log(pagename). Maybe you can find the error here.
If pagename looks good you can try with concatination:
pageName = 'somePage' + '.js';
const page = require('../../cypress/model/page/web/app/' + pageName);

Using encodeURIComponent in JavaScript to encode URL variable, but then decode to write?

I've got a weird situation where I'm trying to dynamically construct a URL string in an XML document using JavaScript. I've figured out how to use encodeURIComponent to assemble my URL parameters using ampersands in a string and construct to the URL that I need. However, I need to write the URL to the page as a decoded string because otherwise, visiting that URL doesn't execute the actions I need to correctly. The whole document is constructed in JavaScript, so it has to be in a JavaScript environment.
For instance, I have the following URL that I'm able to assemble in order for the XML to render correctly:
http://www.myurl.com/product-name?itemcolor=2%26size=7
However, I need to turn it back into this as a link in the XML doc:
http://www.myurl.com/product-name?itemcolor=2&size=7
Is that possible? Does that make sense?
Here's my code snippet so far:
var baseURL = 'http://myurl.com', urlappend = '', colorid, sizeid;
function getVariants(item) {
variants = '<variant>';
if ("custitem109" in item['columns']){
colorid = item['columns']['custitem109']['internalid'];
urlappend += '?itemcolor=' + colorid;
}
if ("custitem110" in item['columns']){
sizeid = item['columns']['custitem110']['internalid'];
urlappend += colorid ? encodeURIComponent('&') + 'size=' + sizeid : '?size=' + sizeid;
}
if (typeof item['columns']['urlcomponent'] !== 'undefined' && urlappend !== '') {
variants += '<action_url>' + baseURL + '/' + item['columns']['urlcomponent'] + urlappend + '></action_url>';
}
variants += '</variant>';
return variants;
}
(this function gets looped through multiple times to construct a portion of the XML feed)
This is a NetSuite suitelet if that makes any difference or if anyone has any question as to why this all has to be in a JS environment.
Any help provided would be very much appreciated.
Actually what you want to do is construct your URLs properly and then xml escape them.
e.g:
http://www.myurl.com/product-name?itemcolor=2&size=7
becomes
http://www.myurl.com/product-name?itemcolor=2&size=7
Any free text should be run through nlapEscapeXML
nlapiEscapeXML('http://www.myurl.com/product-name?itemcolor=2&size=7');

How to solve error while parsing HTML

I´m trying to get the elements from a web page in Google spreadsheet using:
function pegarAsCoisas() {
var html = UrlFetchApp.fetch("http://www.saosilvestre.com.br").getContentText();
var elements = XmlService.parse(html);
}
However I keep geting the error:
Error on line 2: Attribute name "itemscope" associated with an element type "html" must be followed by the ' = ' character. (line 4, file "")
How do I solve this? I want to get the H1 text from this site, but for other sites I´ll have to select other elements.
I know the method XmlService.parse(html) works for other sites, like Wikipedia. As you can see here.
The html isn't xml. And you don't need to try to parse it. You need to use string methods:
function pegarAsCoisas() {
var urlFetchReturn = UrlFetchApp.fetch("http://www.saosilvestre.com.br");
var html = urlFetchReturn.getContentText();
Logger.log('html.length: ' + html.length);
var index_OfH1 = html.indexOf('<h1');
var endingH1 = html.indexOf('</h1>');
Logger.log('index_OfH1: ' + index_OfH1);
Logger.log('endingH1: ' + endingH1);
var h1Content = html.slice(index_OfH1, endingH1);
var h1Content = h1Content.slice(h1Content.indexOf(">")+1);
Logger.log('h1Content: ' + h1Content);
};
The XMLService service works only with 100% correct XML content. It's not error tolerant. Google apps script used to have a tolerant service called XML service but it was deprecated. However, it still works and you can use that instead as explained here: GAS-XML
Technically HTML and XHTML are not the same. See What are the main differences between XHTML and HTML?
Regarding the OP code, the following works just fine
function pegarAsCoisas() {
var html = UrlFetchApp
.fetch('http://www.saosilvestre.com.br')
.getContentText();
Logger.log(html);
}
As was said on previous answers, other methods should be used instead of using the XmlService directly on the object returned by UrlFetchApp. You could try first to convert the web page source code from HTML to XHTML in order to be able to use the Xml Service Service (XmlService), use the Xml Service as it could work directly with HTML pages, or to handle the web page source code directly as a text file.
Related questions:
How to parse an HTML string in Google Apps Script without using XmlService?
What is the best way to parse html in google apps script
Try replace itemscope by itemscope = '':
function pegarAsCoisas() {
var html = UrlFetchApp.fetch("http://www.saosilvestre.com.br").getContentText();
html = replace("itemscope", "itemscope = ''");
var elements = XmlService.parse(html);
}
For more information, look here.

How to add an variable value in between the hyperlink in javascript

I am sending an email via javascript. In mail.parameter.text property I need to send the hyperlink. In the code below I am hardcoding the url whick looks very lengthy and also I need to add the /dashboard at the end of the url. Any idea on how to shorten the url?
var parent = space.getParent();
var siteGroup = "GROUP_EMAIL_CONTRIBUTORS";
var mail = actions.create("mail");
mail.parameters.to_many = siteGroup;
mail.parameters.subject="New Site Created in Community"
mail.parameters.text=" A new site called " + document.properties.name +"is created.Click http://pc1:8080/share/page/site/"+document.properties.name+"/dashboard"+" to join the site";
//execute action against a document
mail.execute(document);
Sorry if this is a basic question I dont know javascript. This is my entire script and I am not using any html tags in between.
You can change it to something like this, for example:
mail.parameters.text="Click <a href='http://pc1:8080/share/page/site/" + document.properties.name + "/dashboard'>here</a>";
Your link will appear as "here" and nevertheless lead to the URL you specified. Is that what you mean by "shorten the url"?
Please try now.
var url = "Click http://pc1:8080/share/page/site/" + document.properties.name + "/dashboard" + " to join the site";
ail.parameters.text = url;

Categories

Resources