Getting string from localization javascript file - javascript

I have a problem with my mobile app using Cordova. I have a strings.js, this file contains all strings used in the app in 2 languages for example :
kStrings["fr"]["skip"] = "passer";
kStrings["en"]["skip"] = "skip";
when I need this string for example I call it like this :
{#skip#}
the problem is when the page is loaded or when I click on this link the text value change to:
{#skip#}
Any ideas?

Related

My javascript code can't recognise my array element defined in Python

So I'm trying to open a new window by executing a script in Selenium using driver.execute_script("window.open('');")
But I want to open a link given by the user.
So I got the link input from my array and put it to my javascript code just like this:
driver.execute_script("window.open(data[0]);")
Now It's giving an error like this:
selenium.common.exceptions.JavascriptException: Message: javascript error: data is not defined
How to fix this? Thanks for your time.
EDIT: A part of my code is something like that:
from selenium import webdriver
import PySimpleGUI as sg
import time
global data
data = []
layouts = [[[sg.Text("Enter the Wordpress New Post link: "), sg.InputText(key=0)]],
[sg.Button('Start The Process'), [sg.Button('Exit')]]]
window = sg.Window("Title", layouts)
def selenium_process():
# Getting the driver path
driver = webdriver.Chrome(r'Driver\path')
driver.get('https://google.com')
driver.execute_script(f"window.open({data[0]});")
time.sleep(10000)
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
data.append(values[0])
selenium_process()
did you try string interpolation ?
Try this:
driver.execute_script(f"window.open({data[0]});")
Your solution does not work since data[0] is a string, not a variable. You instead need to substitute data[0] with its value (must be a value that JS can understand).
Please read the description of Javascript window.open : https://developer.mozilla.org/fr/docs/Web/API/Window/open
If you just need to get to an URL:
driver.get(data[0])

reference javascript content coming from db instead of static file

I am developping an asp.net web application :
In the website folder, I have a folder called "integrations" containing a list of js files (name1.js, name2.js, name3.js ...)
A user makes a http request to a mvc controller method where he gives as input a "name".
This method gives the "name" as a property of a viewmodel to a razor view containing the following code :
This razor view is returned to the user.
The previous code is working well, but I would like to add an improvment. Actually you need to add a js file inside the folder integrations,
and you cannot do that when the application is running in production.
So I would like instead of having a script tag referencing a file placed inside the integrations folder to have a script tag containing a js content
coming from a data table like this :
name : jscontent
name1 : jscontent1
name2 : jscontent2
But I don't know how to changed this :
<script src="~/integrations/#(Model.Name).js"></script>
To :
<script>>query in db by #(Model.Name) parameter to get corresponding jscontent</script>
Make it a controller/method and you can take advantage of things like caching, auth etc for free:
public class ScriptController
{
public ActionResult Content(MyModel model)
{
var result = "alert("Hello World!");";
return JavaScript(result);
}
}
JavaScriptResult
Then in your html:
<script src="~/Script/Content?prop1=whatever"></script>

Java script not working on application page

I have wrote a java script to replace the value of email field to "user name" same time when user tying in the email field and I hided the username field.
Used string builder to render the script
function change(){
var Email=document.getElementByID('#Email');
var UserName=document.getElementByID('#UserName);
UserName.value=Email.Value;
}
the above code is working only when I add the webpart inside sharepoint page,but when I added the web part on an application page the UserName field is not getting updated when email field is typed.
when checked browser debugging I cant see the java script in that application page .
can I how to work this is application page ,Do I need to copy the same script to the application page where I added the webpart?
Thanks
There was two problems here, the first, getElementById wasn't written well and second, value property is lowercase.
function change(){
var Email = document.getElementById('#Email');
var UserName=document.getElementById('#UserName');
UserName.value=Email.value;
}
Issue 1 : it should be "getElementById" not getElementByID
Issue 2 : The id should not have # until and unless you are using the ID that has #(sounds silly to use # in Id)
Issue 3 : it should be "value" not Value
Use an IDE like visual studio and you will not get these silly errors.

How can I get the url working from js html builder in Django?

I am currently building a web app using Django.
I've build a calendar using JS and I am building HTML basically from the Js File.
I am trying to include hrefs in each calendar day. For example :
{% url 'calendarDay' day=28 month=12 year=2016 %} is what should have for 28/12/2016 date.
If I try to go to this url from anywhere else in my templates it works. For some reason it is not working when I pass the html from js.
This is what I have in my urls:
url(r'^calendar/$', views.calendar, name='calendar'),
url(r'^calendar/(?P<day>\w+)/(?P<month>\w+)/(?P<year>\w+)/$',views.calendar, name = 'calendarDay'),
This is my js builder function (the part that builds the previous month's days):
if (FirstDay.getDay()==0){
for (var i=LastMonthDays-5; i <= LastMonthDays; i++) {
href="<a href=\"{% url 'calendarDay' day="+i+" month="+(pastMonth.getMonth()+1)+" year="+pastMonth.getFullYear()+" %}\">"
html +=href+ "<li>"+(i)+"</li></a>";
}
}
This is how the html file looks like when I inspect the page:
<li>28</li>
For some reason the urls that it gets is: http://127.0.0.1:8000/calendar/%7B%%20url%20'calendarDay'%20day=28%20month=12%20year=2016%20%%7D
And ofcourse I get the following error:
The current URL, calendar/{% url 'calendarDay' day=28 month=12 year=2016 %}, didn't match any of these.
What might be the issue here?
Templatetags will be rendered in server side, before javascript. you should create a javascript function for generating your calendar urls.

Create a textfile from the input of an html-form with the browser (client-side)

I have an html site with a form in it and I want the user to be able to create a text/xml file depending on the input. But I wan't to avoid setting up a webserver only for this task.
Is there a good way, to do that, e.g. with Javascript? I think you can't create files with Javascript, but maybe create a data url and pass the text, so the user can save it to file?
Or is there another way to achieve this simple task without a webserver?
Solved it, somehow. I create a data url data:text/xml;charset=utf-8, followed by the XML.
function createXML() {
var XML = 'data:text/xml;charset=utf-8,<MainNode>';
var elements = document.getElementsByTagName('input'),i;
for (i in elements) {
if (elements[i].checked == true) {
XML += elements[i].value;
}
}
XML += '</MainNode>';
window.open(XML);
}
So the url looks like data:text/xml;charset=utf-8,<MainNode><SubNode>...</SubNode>...</MainNode>
Unfortunately this doesn't work for me on Chromium(Chrome) and on Firefox. It just displays the XML instead of showing a save dialog. But I think that's because of my settings and at least you can save it as a XML-file manually.
I haven't tried this but it should work.
After getting form data, system will call page A.
page A will have javascript that gets query strings and builds the page accordingly.
After finishing page build, user can save current page with following statement in javascript
document.execCommand('SaveAs',true,'file.html');

Categories