I need to get the data of productSkus[ using python but I don't know how to access it. the javascript comes from http://www.ulta.com/lid-lingerie-eye-tint?productId=xlsImpprod15361061 .
This is how it looks like.
<script type="text/javascript">
var currentSkuId = '2502111';
var currentProductId = 'xlsImpprod15361061';
var productSkus = new Object();
productSkus[2502111] =
{"displayName":"Fame & Fortune","id":"2502111","imgUrl":"http://images.ulta.com/is/image/Ulta/2502111?$detail$","largeImgUrl":"http://images.ulta.com/is/image/Ulta/2502111?$lg$","swatchImgUrl":"http://images.ulta.com/is/image/Ulta/2502111sw?50ドルpx$","swatchHoverImgUrl":"http://images.ulta.com/is/image/Ulta/2502111sm?$md$","skuSize":"0.13","skuSizeUOM":"oz"};........
Can anyone help me with this?
-
1Python runs on the server, Javascript runs on the client. You need to use AJAX to send the JS variable to the server.Barmar– Barmar2017年07月04日 07:19:33 +00:00Commented Jul 4, 2017 at 7:19
-
Your question does not even make clear where the Javascript code snippet comes from and what you want to do with it. Is this your Python application or a page from a third party source you are trying to parse.Klaus D.– Klaus D.2017年07月04日 07:27:34 +00:00Commented Jul 4, 2017 at 7:27
-
just edited my question.Zhuoyang Li– Zhuoyang Li2017年07月04日 07:34:32 +00:00Commented Jul 4, 2017 at 7:34
2 Answers 2
If you want to use productSkus on the server side, then you need to use AJAX to send the JS variable to the server.
As Django template is compiled server side. It is then sent to the client where their browser executes the JavaScript. Nothing that is changed by the JavaScript executing on the client browser can have an affect on the template. It's too late at that point.
However the JavaScript could do something like make another request from the server for more information. Or you could just pre-compute the value on the server before you send it to the client.
You can of course use Django templates to set JavaScript variables.
<script>
var myVar = '{{ py_var }}';
</script>
Comments
use html form to submit the data to server, or across api