Categories
discuss

jQuery resize event not firing

For whatever reason the following:

$(function() {
  $(window).resize(function() {
    alert("resized!");
  });
});

only fires an event when the page is loaded. Resizing the browser window does nothing in both Safari and Firefox. I have not tried it on any other browsers.

Any ideas or workaround?

Answer

I think your alert is causing a problem try this instead

 $(window).resize(function() {
   $('body').prepend('<div>' + $(window).width() + '</div>');
 });

jsfiddle

Categories
discuss

How to get specific element in JSON

If I have JSON that is:

var response = {results: 2938; id: 9283};

How can I get id using javascript/jquery?

I tried something like but I can’t seem to get what I want (I want to get the number in id):

response[1]

and

response[1].id

Answer

Simple:

response.id

With that being said, your json is invalid,

var response = {results: 2938; id: 9283};

Use a , to separate items not a ;

var response = {results: 2938, id: 9283};

And since I love jsfiddle so much, here is an example.

Categories
discuss

How does an application that uses Spring’s SimpleNamingContextBuilder know to search its directory for resources?

How does an application that uses Spring’s SimpleNamingContextBuilder as its JNDI provider know to search its directory for resources? What links the application to the Spring naming directory? For example, how does the JndiObjectFactoryBean bean in this earlier answer know to find resource my-db in the Spring directory? Doesn’t JndiObjectFactoryBean require a context environment with property java.naming.factory.initial set to some implementation of interface InitialContextFactory? What should the value of java.naming.factory.initial be when using SimpleNamingContextBuilder as the JNDI provider?

Answer

Java runtime class NamingManager serves as the link between a Java application and its naming directory. When a SimpleNamingContextBuilder activates, it installs itself to static member InitialContextFactoryBuilder in NamingManager. When the application creates an InitialContext to retrieve the JNDI context, class InitialContext delegates to NamingManager, which in turn asks the IntialContextFactoryBuilder (in this case, SimpleNamingContextBuilder) to create an IntialContextFactory, which ultimately creates the InitialContext.

JndiObjectFactoryBean doesn’t need an explicit context environment because SimpleNamingContextBuilder provides the InitialContextFactory to the NamingManager and JndiObjectFactoryBean uses the NamingManager to retrieve its resources. So, in the earlier answer, JndiObjectFactoryBean “knows” to search the Spring naming directory for resource my-db because SimpleNamingContextBuilder has established itself as the JNDI provider in the NamingManager.

Categories
discuss

Combine jQuery and Zen-Coding php ports to emulate client side programming style on server side scripts

When I write client side code, I use HTML/CSS/JavaScript and lately jQuery to both speed up coding, and use improved methods to achieve the same goal.

In my text editor I use zen-coding to speed up the writing of code, and also to avoid errors. I was looking at zen-coding as a jQuery plugin for a while, but it has a fatal flaw, that you want the HTML to be written and sent to the client plain before any javascript kicks in.

Although we can use JavaScript servers (env.js or node.js) and therefore do a lot of development server side using JavaScript and jQuery, I am not comfortable moving over yet as it is an emerging technology, and has many differences and drawbacks (and also some major advantages).

I want to continue using PHP server side, but develop in the way I am most comfortable with, and familiar with which is client side JavaScript.

Therefore – I have been looking into QueryPath which is a PHP port of jQuery that aims to take the best and most relevant parts of jQuery and re-work it to suit the server environment.

That is all great, and I have now been looking at two PHP classes capable of parsing zen-coding which when combined acts as a great templating engine and also avoids errors in my code.

The problem I am having is that neither zen-coding parsers support anywhere near a full set of zen-coding features.

So finally my questions (sorry for the rather lengthy intro)

  1. Is there a better server side zen-coding parser I can use in my PHP code?
  2. Is there a good (very concise and full featured) alternative templating system to using zen-coding? (which I know is not originally designed for this task)
  3. Is there a better approach I should take to achieve my ultimate goal of narrowing the divide between the way I code client side and server side?
  4. Is there a PHP library that implements a load of utility functions that by using will enhance the security/performance of my code without me learning all the internal workings? (like jQuery does for javascript)

NB: I am looking more for functional equivalence than syntactic similarity – although both are a plus for me.

Here is some commented test code that should illuminate what I am trying to achieve:

<?php

    // first php based zen-coding parser
    // http://code.google.com/p/zen-php
    require_once 'ZenPHP/ZenPHP.php';
    // my own wrapper function
    function zp($abbr){ return ZenPHP::expand($abbr); }

    // second php based zen-coding parser
    // https://github.com/philipwalton/PW_Zen_Coder
    require_once 'PW_Zen_Coder/PW_Zen_Coder.php';
    $zc = new PW_Zen_Coder;
    // my own wrapper function
    function pwzc($abbr){ global $zc; return $zc->expand($abbr); }

    // php port of jQuery with a new server-side flavor
    // http://querypath.org/
    require_once 'QueryPath/QueryPath.php';

    // initialize query path with simple html document structure
    qp(zp('html>head+body'))

        // add a heading and paragraph to the body
        ->find('body')
        ->html(zp('h1{Zen Coding and jQuery - Server Side}+p{This has all been implemented as a php port of JavaScript libraries}'))

        // add a comments link to the paragraph
        ->find('p')
        ->append(pwzc('span.comments>a[href=mailto:this@comment.com]{send a comment}'))

        // decide to use some jquery - so add it to the head
        ->find(':root head')
        ->append(zp('script[type=text/javascript][src=/jquery.js]'))

        // add an alert script to announce use of jQuery
        ->find(':root body')
        ->append(zp('script[type=text/javascript]{$(function(){ alert("just decided to use some jQuery") })}'))

        // send it to the browser!
        ->writeHTML();

    /* This will output the following html

    <html>
    <head>
    <script type="text/javascript" src="/jquery.js"></script>
    </head>
    <body>
    <h1>
        Zen Coding and jQuery - Server Side
    </h1>
    <p>
        This has all been implemented as a php port of JavaScript libraries
    <span class="comments">
        <a href="mailto:this@comment.com">

            send a comment
        </a>
    </span>
    </p>
    <script type="text/javascript">
        $(function(){ alert("just decided to use some jQuery") })
    </script>
    </body>
    </html>

    */
?>

Any help is much appreciated

Answer

first of all i want to say i have up-voted your answer because it is well explained and have some nice point to consider; then i want let you think about theese other point:

GOTCHAS

  1. IMHO you are overcomplicating the whole thing 😉

  2. between the entire PHP code needed to generate the HTML and the outputted HTML itself there is very very low difference in term of lenght of writed-code.

  3. the code is completely unredeable for everyone who don’t know the 3 libs or whatever it is.

  4. the speed of site-load will decrease enourmously compared to the semplicity of the vanilla HTML.

  5. what the real difference between:


h1{Zen Coding and jQuery - Server Side}+p{This has all been implemented as a php port of JavaScript libraries}

and

<h1>Zen Coding and jQuery - Server Side</h1><p>This has all been implemented as a php port of JavaScript libraries</p>

6.. as you know both zen-coding and queryPath are not intended to be used the way you are doing, at least not in a production scenario.

7.. The fact that jQuery have a good documentation and it’s usefull to use doesn’t mean that can be used successfully from anyone. ( the mere copy/past is not considered a coding skill IMO )

SOLUTION

it is probably the best solution for you looking at some kind of PHP Templating Engine like smarty, this will suit your needs in various way:

  1. security/performance
  2. narrowing the divide between the way I code client side and server side

an example would be: ( to be considered a very primitive example, smarty have more powerfull functionalities )

<!-- index.tpl -->
<html>
  <head> {$scriptLink} 
  </head>
  <body> <h1> {$h1Text} </h1>
    <p> {$pText} 
      <span class="comments">
        <a href="{$aLink}"> {$aText} </a>
      </span>
    </p> {$scriptFunc} 
  </body>
</html>

    // index.php
    require('Smarty.class.php');
    $smarty = new Smarty;
    $smarty->assign("scriptLink", "<script type="text/javascript" src="/jquery.js"></script>");
    $smarty->assign("scriptFunc", "<script type="text/javascript">$(function(){ alert("hello world") });</script>");
    $smarty->assign("h1Text", "Zen Coding and jQuery - Server Side");
    $smarty->assign("pText", "This has all been implemented as a php port of JavaScript libraries");
    $smarty->assign("aText", "send a comment");
    $smarty->assign("aLink", "mailto:this@comment.com|mailCheck");
    $smarty->display('index.tpl');

NOTE: the use of mailCheck, yes you should also consider eventuality some kind of variable check. smarty can do it….

hope this help. 😉

Categories
discuss

Sending Wake on LAN packet from Android to PC

My Android app sends/retrieves data to/from the user’s own PC using HTTP and it’s working fine with a handful of beta testers. I now need to consider a situation where the PC is hibernating.

I’ve never done this before but I’ve googled to find info about the WOL ‘magic packet’ and some simple source written in C (using CAsyncSocket at the client end). Doing this over a wi-fi connection on the user’s home network is likely to be relatively straight-forward but ideally I want this to work over mobile internet (assuming the user can configure their home router to accept / forward the packet).

I’m guessing I need to use some generic Java network code and I’ve been looking at java.net.

At this point I can’t decide whether I should be using java.net.Socket or java.net.DatagramSocket. So the question is, am I approaching this the right way and which of the two socket types should I be using (or would both suffice)? Many thanks.

Answer

Here is some C# code that I have used in the past. It should be relatively easy to convert into java and send using a DatagramPacket

namespace WakeOnLan
{
    class Program
    {
        static void Main(string[] args)
        {

            byte[] mac = new byte[] { mac address goes here i.e 0x00, and so on };
            WakeUp(mac);
        }

        private static void WakeUp(byte[] mac)
        {
            //
            // WOL packet is sent over UDP 255.255.255.0:40000.
            //
            Console.WriteLine("Waking Up.......");
            UdpClient client = new UdpClient();
            client.Connect(IPAddress.Broadcast, 40000);

            //
            // WOL packet contains a 6-bytes trailer and 16 times a 6-bytes sequence containing the MAC address.
            //
            byte[] packet = new byte[17 * 6];

            //
            // Trailer of 6 times 0xFF.
            //
            for (int i = 0; i < 6; i++)
                packet[i] = 0xFF;

            //
            // Body of magic packet contains 16 times the MAC address.
            //
            for (int i = 1; i <= 16; i++)
                for (int j = 0; j < 6; j++)
                    packet[i * 6 + j] = mac[j];

            //
            // Submit WOL packet.
            //
            client.Send(packet, packet.Length);
            Console.WriteLine("Machine Woke Up....");
        }
    }
}

Hope this helps

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..