1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 23:08:11 +00:00

LibWeb: Allow navigating to a new URL by setting window.location.href

This commit is contained in:
Andreas Kling 2020-05-18 21:52:50 +02:00
parent 1ec4db04cd
commit efdfdbabdb
5 changed files with 32 additions and 3 deletions

View file

@ -2,9 +2,13 @@
<html> <html>
<head> <head>
<title>window.location test</title> <title>window.location test</title>
<style>
#clickme { background-color: red; color: yellow; }
</style>
</head> </head>
<body> <body>
<pre></pre> <pre></pre>
<div id="clickme">Click me to navigate away!</div>
<script> <script>
var pre = document.querySelectorAll("pre")[0]; var pre = document.querySelectorAll("pre")[0];
pre.innerHTML += "href: " + location.href + '\n'; pre.innerHTML += "href: " + location.href + '\n';
@ -12,6 +16,10 @@
pre.innerHTML += "pathname: " + location.pathname+ '\n'; pre.innerHTML += "pathname: " + location.pathname+ '\n';
pre.innerHTML += "hash: " + location.hash + '\n'; pre.innerHTML += "hash: " + location.hash + '\n';
pre.innerHTML += "search: " + location.search + '\n'; pre.innerHTML += "search: " + location.search + '\n';
document.getElementById("clickme").addEventListener("mousedown", function() {
window.location.href = 'http://serenityos.org/';
});
</script> </script>
</body> </body>
</html> </html>

View file

@ -54,9 +54,13 @@ JS::Value LocationObject::href_getter(JS::Interpreter& interpreter)
return JS::js_string(interpreter, window.impl().document().url().to_string()); return JS::js_string(interpreter, window.impl().document().url().to_string());
} }
void LocationObject::href_setter(JS::Interpreter&, JS::Value) void LocationObject::href_setter(JS::Interpreter& interpreter, JS::Value value)
{ {
// FIXME: Navigate to a new URL auto& window = static_cast<WindowObject&>(interpreter.global_object());
auto new_href = value.to_string(interpreter);
if (interpreter.exception())
return;
window.impl().did_set_location_href({}, new_href);
} }
JS::Value LocationObject::pathname_getter(JS::Interpreter& interpreter) JS::Value LocationObject::pathname_getter(JS::Interpreter& interpreter)

View file

@ -30,7 +30,10 @@
#include <LibJS/Interpreter.h> #include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Function.h> #include <LibJS/Runtime/Function.h>
#include <LibJS/Runtime/MarkedValueList.h> #include <LibJS/Runtime/MarkedValueList.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Window.h> #include <LibWeb/DOM/Window.h>
#include <LibWeb/Frame.h>
#include <LibWeb/HtmlView.h>
namespace Web { namespace Web {
@ -105,4 +108,15 @@ void Window::cancel_animation_frame(i32 id)
GUI::DisplayLink::unregister_callback(id); GUI::DisplayLink::unregister_callback(id);
} }
void Window::did_set_location_href(Badge<Bindings::LocationObject>, const String& new_href)
{
auto* frame = document().frame();
if (!frame)
return;
auto* view = frame->html_view();
if (!view)
return;
view->load(new_href);
}
} }

View file

@ -49,6 +49,8 @@ public:
void set_interval(JS::Function&, i32); void set_interval(JS::Function&, i32);
void set_timeout(JS::Function&, i32); void set_timeout(JS::Function&, i32);
void did_set_location_href(Badge<Bindings::LocationObject>, const String& new_href);
private: private:
explicit Window(Document&); explicit Window(Document&);

View file

@ -67,6 +67,7 @@ class EventTargetWrapper;
class HTMLCanvasElementWrapper; class HTMLCanvasElementWrapper;
class HTMLImageElementWrapper; class HTMLImageElementWrapper;
class ImageDataWrapper; class ImageDataWrapper;
class LocationObject;
class MouseEventWrapper; class MouseEventWrapper;
class NodeWrapper; class NodeWrapper;
class WindowObject; class WindowObject;