douhan0562 2013-12-05 08:11
浏览 19
已采纳

将内容推送到浏览器...更改DOM?

Ok. I have this passion project I've been working on.

I'm at the stage where I have a mobile app and a backend, really I use a hosted messaging/cloud processing service as my NEW backend, replacing my own servers.

The problem lies in my non ability to understand javascript.

I know how to go from mobile to backend and all that good stuff... What i now what to do is simply change a DOM element. From either GO code or Python code.

mobile ----> messaging/processing ----> browser/DOM

Basically push either a url or a string of html code so that it displays in the browser

For this I understand my only choice is javascript.

I've looked at pusher and web sockets...

but i still dont understand how to ACTUALLY make the html element change.

  • 写回答

3条回答 默认 最新

  • doujuchuan9915 2013-12-05 09:02
    关注

    Since you have considered Pusher, you can get the necessary keys from Pusher. Once you have access to that, in the client, you can add the Pusher js library to the html file you want to "push" events to.

    Then you need to subscribe to a channel(js).

    var channelName = "my-channel"
    var channel = pusher.subscribe(channelName);
    

    Once you subscribe to a channel from client. Trigger an event along with the data you need to send using python pusher server lib (python):

    push = pusher.Pusher(app_id='your-pusher-app-id', key='your-pusher-key', secret='your-pusher-secret')
    push['my-channel'].trigger('dom-change-event', {'data': 'data'})
    

    You can listen to this event from your client side(js):

    channel.bind("dom-change-event", function(data){ // the data you Pushed from backed
      $(".div-to-change").hide(); // with the help of jQuery select the dom, and hide it.
    })
    

    Pusher maintains a websocket connection with the client, and as soon as an event is triggered from the server, it is raised by Pusher at the client side.

    DOM manipulation(change):

    The DOM change that you were referring to can be done in many ways. Some are mentioned below:

    1. Have all the Content(html code) ready in your frontend and based on the server's push message you can show/hide different parts of your DOM like the one mentioned above.
    2. Construct the DOM on the fly with javascript based on the pusher message
    3. Send the HTML code from the server and simply append it to the DOM.

    IMPORTANT thing to note here is that DOM manipulation is done using a library called jQuery in the above example. jQuery makes it easy for you do DOM manipulations. http://jquery.com/

    Also refer to this article to get started on doing DOM manipulations(changes) using jQuery.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路