I am trying to implement server side code on client side. "trvddl1" is a ascx control which contains a dropdownlist "ddltree". It was easy on server side but I am facing difficulty using the same in javascript.
How do I write the following code in javascript?
((DropDownList)trvddl1.FindControl("ddltree")).SelectedValue;
I tried
var abc = document.getElementById('<%=trvddl1.ClientID%>').value;
and
var Region = document.getElementById('<%=trvddl1.FindControl("ddltree")%>').value;
but javascript returned error. Is there some other keyword I am missing ?
Check the HTML output (Browser-->View Source) and locate the control there, see what the ID of that control has, and put that one into the getElementById() function.
Example:
<input id='ddltree' .... />
Then use:
var abc = document.getElementById('ddltree').value;
Perhaps you can try something like that:
// find all controls that have an id that ends with ddltree
// starts with would be [id*=ddltree]
var abc = document.querySelectorAll("[id$=ddltree]");
if(abc.length > 0) {
// got it !
console.log(abc[0].value);
}
Please note that querySelectorAll is not supported in all browsers (even though - most). Here is a reference.
Related
I am using simple_html_dom to scrape some webpages and I need help. Here is my Javascript:
<script type="text/javascript">
var session = "";
var order = "1";
var status = "";
var json_product = [{bla bla bla...}];
</script>
So I want to get inside var json_produk = ... is meaning:
[{"bla.... bla... bla... until }];
How can I get it?
I am trying for:
$html->find('var', 2);
I just get a 500 error because find use for find string I think.
I am using [an HTML parser] to scrape some webpages
But the value you want is in javascript (which is embedded in the HTML).
While you can retrieve the entire block of javascript with the DOM parser, that's as much resolution as it will give you.
$script=$html->find('script') . ''; // not tested - YMMV
PHP does not have a Javascript parser. If you can be sure that the JSON will always be assigned to "json_product" and that there is no further javascript code, then you could.....
$srch='json_product =';
$start=strpos($script, $srch);
$json=substr($script, $start+strlen($srch)+1);
$data=json_decode($json);
....but this is a long way from a robust solution.
I just get a 500 error because find use for find string I think.
That should not cause "a 500 error"
I need custom javascript code for Google tag manger to create variable for transaction value. To do this I need a code that return number 30.99 from HTML:
<span class="price-data">zł30.99</span>
I wrote some code but it doesn’t work:
function() {
var orderValue = document.getElementsByClassName(‘price-data')[0].value;
return orderValue.match((\d*\.\d*)|(\d*));
}
Could you help me with that
See the following snippet. Check the console value.
var orderValues = document.getElementsByClassName('price-data');
var orderValue = orderValues[0].innerHTML;
var match = orderValue.match(/([\d\.]+)/);
console.log(match[0]);
<span class="price-data">zł30.99</span>
"I need custom javascript code for Google tag manger " - no, you don't. You need a DOM type variable with the selector method set to "CSS selector" and the selector set to your class name. This will return the text content of the first element that matches the selector.
Since GTM already carries a selector engine around you really gain nothing by writing custom Javascript.
I'm creating a project in Adobe Experience Manager and have run into problems in the implementation of my language switching component. The component is supposed allow the user to click on a link and have the language of the page change. For example, if they are on the English page /content/myproject/en/home.html and they click it, they are supposed to end up on /content/myproject/fr_ca/home.html.
As part of getting it up and running, I was trying to concatenate currentPage.path and "/profile.html" so that I could at least get the component to register some change to the string in the tag.
From the English home.html page, currentPage.path produces the string "/content/myproject/en/home". Concatenating it with /profile.html should produce the string "/content/myproject/en/home/profile.html" which it does if I use Sightly to do something like <p>${langinfo.goToPage}</p>.
However, if I try this: the component will show a blank anchor tag. It will also blank anything I've written in between the two anchor tags.
So far I've tried returning a string I've written out by hand "/content/myproject/en/home/profile.html" as the goToPage value and it works in the anchor tag. Also, if I only return currentPage.path it works. It refuses to work like this if I try to concatenate but it will work like this: <a>It works here!.
The best I can figure at this point is that currentPage.path is a Java String object that is being accessed by JavaScript and there are problems when JS tries to type it to a JavaScript string with +. It also doesn't work if I try to cast the statement as a string with either String(goToPage) or goToPage.toString(). It doesn't seem to matter when I cast it as a string. One blog I looked at seemed to hint that this was a problem with Rhino and that I should do a .toString() after the initial concatenation. That didn't work. Another post on stackOverflow seemed to point out that it could be a problem trying to concatenate a Java String object in JavaScript and pointed out that this should be taken into account but didn't go into how to deal with the issue.
I appending to a string isn't the intended end functionality of my component, but if I can't modify the string by concatenating, seems like I can hardly do a search and replace to change /en/ to /fr-ca/. If anyone has a more elegant solution to my problem than what I'm attempting, that would be appreciated as much as a fix for what I'm working on.
I've pasted my code here (as suggested) and posted screenshots of my code to help.
Javascript:
use(function() {
var pageLang = currentPage.properties.get("jcr:language", "en");
var otherLangText;
var currPage = currentPage.name;
var currPagePath = currentPage.path;
var goPage;
if (pageLang == "fr_ca") {
otherLangText = "English";
goPage = "/content/myproject/en/index/home.html";
} else {
otherLangText = "Français";
goPage = "/content/myproject/fr-ca/home/home.html";
};
return {
otherLanguage: otherLangText,
goToPage: goPage
}
})
HTML:
<nav data-sly-use.langinfo="langcontact.js">
<ul class="lang-list-container">
<li class="lang-list-item">${langinfo.otherLanguage}</li>
<li class="lang-list-item">Contact</li>
</ul>
</nav>
I'm pretty stumped here. What am I doing wrong?
The line <li class="lang-list-item">${langinfo.otherLanguage}</li>
should actually be -
<li class="lang-list-item">${langinfo.otherLanguage}</li>
What you are trying to do is pass an object to object which will not work, in case you want to pass the extension to be used in JS you need to do that in the USE call. Refer to the samples in this blog.
Update -
You code works fine for me as long as the link is valid.
use(function() {
var pageLang = currentPage.properties.get("jcr:language", "en");
var otherLangText;
var currPage = currentPage.name;
var currPagePath = currentPage.path;
var goPage;
if (pageLang == "fr_ca") {
otherLangText = "English";
goPage = currPagePath+"/profile.html";
} else {
otherLangText = "Français";
goPage = currPagePath+"/profile.html";
};
return {
otherLanguage: otherLangText,
goToPage: goPage
}
})
The only possible reason you are getting empty href is because your link is not valid and thus linkchecker is removing it. If you check on author instance you will see broken link symbol along with your link text.
Ideally you should fix the logic so that proper valid link is generated. On development you could disable the linkchecker and/orlink transformer to let all links work (even invalid ones | not recommended). The two services can be checked in http://localhost:4502/system/console/configMgr by searching for - Day CQ Link Checker Service and Day CQ Link Checker Transformer
I'm doing a simple jQuery.get call to api.facebook.com to retrieve the number of likes for a URL. The result is in XML (which is unexpected in itself, but not the point here). The response looks like this:
<links_getStats_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd" list="true">
<link_stat>
<url>godfisk.no/fiskenasjonen</url>
<normalized_url>http://www.godfisk.no/fiskenasjonen</normalized_url>
<share_count>35</share_count>
<like_count>402</like_count>
<comment_count>10</comment_count>
<total_count>447</total_count>
<click_count>0</click_count>
<comments_fbid>676768902366174</comments_fbid>
<commentsbox_count>0</commentsbox_count>
</link_stat>
</links_getStats_response>
To traverse this tree and get to the text content of the <like_count> element, I'm doing this:
data.firstChild.childNodes[1].childNodes[7].textContent;
This bring me back to the way XML was handled in AS3 prior to E4X. I cannot help but feel that this is pretty vulerable, so I decided it would be a good idea to validate the integrity of the returned data before accessing it. That leaves me with this Christmas tree compound if-statement:
if (data
&& data.childNodes.length > 0
&& data.firstChild.childNodes.length > 1
&& data.firstChild.childNodes[1].childNodes.length > 7
&& data.firstChild.childNodes[1].childNodes[7].textContent) {
// OK, we're good, go ahead..
}
Really??
What you need to do is use XPath, so for example to get to like_count element in your example:
var elem = data.selectSingleNode('/links_getStats_response/link_stat/like_count'); // assign the element
var value = elem.nodeValue; // to get the element value
for more information on XPath, please see the following link
A bit cheeky, but really the better option since I'm already using jQuery:
var numLikes = $('like_count', data).text();
Or if trying to avoid jQuery at all costs:
var numLikes = xml.querySelector('like_count').textContent;
when i use user defined tags with uppercase node like "<ABC> test </ABC>" in ckeditor .On clicking source, it gets displayed as "<abc> test </abc>".please help me to get the expected output , which should be <ABC> test </ABC> and please guide me where the code should be modified.Thanking you
(Continued from comments) I propose post-processing the content and not trying to bend CKEditor to produce Case Sensitive output.
I don't know your languages or your architecture, but if you get the data from CKEditor with getData(), you can do something like this if you want to do the conversion in the client side:
// Javascript
var i = CKEDITOR.instances.editor1;
var d = i.getData();
var correctData = d.replace(/<abc/ig, '<ABC');
In the backend you can do something similar
// C# (untested)
string result = Regex.Replace(
htmlStringFromAJAX,
RegEx.Escape("<abc"),
RegEx.Escape("<ABC"),
RegexOptions.IgnoreCase
);
// PHP (untested)
$result = str_ireplace("<abc", "<ABC", $htmlStringFromAJAX);
(I hope you either have just this one abc tag or a small static amount of tags - if not, this will be a very annoying solution to maintain.)