Send argument in Javascript Use API from HTML | Community
Skip to main content
Level 6
June 1, 2021
Solved

Send argument in Javascript Use API from HTML

  • June 1, 2021
  • 1 reply
  • 3784 views

I have a js file with the following code:

"use strict";
use(function () {

//var url= this.url; //line 3
var url = "http://experienceleague.adobe.com/docs/experience-manager-htl/using/htl/use-api-javascript.html?lang=en"; //line 4
var getRequest = new org.apache.commons.httpclient.methods.GetMethod(url),
client = new org.apache.commons.httpclient.HttpClient(),
status = new org.apache.commons.httpclient.HttpStatus();
var info = {};
var statusCode= client.executeMethod(getRequest);
if (statusCode != 404)
info.code = true;
else
info.code = false;
return info;
});

HTML:

<template data-sly-use.item="java model path">

<sly data-sly-use.status="'myfile.js' @url='${item.url}'">

</template>

 

The above code works fine for the hard coded value of URL in JS line 4. Now if i uncomment line 3 and comment out line 4 from my JS. The code breaks.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by MayurSatav

Hi @shaheena_sheikh ,

I checked the above code and I think it is because of the synchronous request in

 

http.open('HEAD', url, false);

 

If this value is false, the send() method does not return until the response is received and this might be the case parsys stops loading. If possible could you please share more details so that I can debug it?

 

Edited

To sent URL as a parameter, you should use data

 

 

<div data-sly-use.status='myfile.js' @ url=${file2.path}" data-example="${status.aFunction}" />

 

 

you can access arguments by using 'this.'

 

 

use(function () { var url = this.url; . . . });

 

 

 

2nd method

access data variable attribute through class

 

<div class="myClass" data-sly-use.status="myfile.js" data-url="${file2.path}"> in HTMLvar args= $(".myClass"); var url= args.data("url");

 

Thank you.

1 reply

MayurSatav
Community Advisor and Adobe Champion
MayurSatavCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
June 1, 2021

Hi @shaheena_sheikh ,

I checked the above code and I think it is because of the synchronous request in

 

http.open('HEAD', url, false);

 

If this value is false, the send() method does not return until the response is received and this might be the case parsys stops loading. If possible could you please share more details so that I can debug it?

 

Edited

To sent URL as a parameter, you should use data

 

 

<div data-sly-use.status='myfile.js' @ url=${file2.path}" data-example="${status.aFunction}" />

 

 

you can access arguments by using 'this.'

 

 

use(function () { var url = this.url; . . . });

 

 

 

2nd method

access data variable attribute through class

 

<div class="myClass" data-sly-use.status="myfile.js" data-url="${file2.path}"> in HTMLvar args= $(".myClass"); var url= args.data("url");

 

Thank you.

Level 6
June 1, 2021

I have updated the code. Now, this part of code work, but i want to send argument to my HTL Javascript Use api function